文章目录
UITableView的基本概念
UITableView继承于UIScrollView,所以UITableView也可以使用UIScrollView的代理方法。在使用前记得添加UITableView的俩个协议<UITableViewDelegate, UITableViewDataSource>。
UITableView的懒加载
- (UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.backgroundColor = [UIColor whiteColor];
//代理
_tableView.delegate = self;
_tableView.dataSource = self;
//分割线
_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
_tableView.separatorColor = UIColor.blueColor;
//标识线
_tableView.showsVerticalScrollIndicator = NO;
_tableView.showsHorizontalScrollIndicator = NO;
//cell注册
[_tableView registerClass:[CellClass class] forCellReuseIdentifier:NSStringFromClass([CellClass class])];
}
return _tableView;
}
UITableView主要用到的有两种样式,UITableViewStylePlain
可以起到让区头悬浮的作用。UITableViewStyleGrouped
区头会随着cell滑动。
separatorStyle
属性:设置cell之间的分隔线样式。
separatorColor
属性:设置分隔线颜色。
showsVerticalScrollIndicator
、showsHorizontalScrollIndicator
属性:设置表的水平、垂直滚动条是否显示。
UITableView的代理方法
@required 必须实现的
区里有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return rowCount;
}
cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CellClass *cell = [[CellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([CellClass class])];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
selectionStyle
属性:设置cell选中样式。
UITableViewCellSelectionStyleNone
UITableViewCellSelectionStyleBlue
UITableViewCellSelectionStyleGray
UITableViewCellSelectionStyleDefault
accessoryType
属性:设置cell右边指示样式。
UITableViewCellAccessoryNone 没有
UITableViewCellAccessoryDisclosureIndicator 箭头
UITableViewCellAccessoryDetailDisclosureButton 感叹号和箭头
UITableViewCellAccessoryCheckmark 对勾
UITableViewCellAccessoryDetailButton 感叹号
@optional 非必须实现的
表里有多少区(默认为1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return sectionCount;
}
区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return headerHeight;
}
区头View
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerView = [[UIView alloc] init];
return headerView;
}
区尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return footerHeight;
}
区尾View
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *footerView = [[UIView alloc] init];
return footerView;
}
行点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"行点击");
}
表的索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray *array = @[@"A",@"B",@"C",@"D"];
return array;
}
UITableView 常用方法
刷新tableView数据
[self.tableView reloadData];
解决iOS15以后区头留白的问题
if (@available(iOS 15.0, *)) {
self.tableView.sectionHeaderTopPadding = 0;
}