一,常规使用
创建对像,设置代理
_table = [[UITableView alloc]initWithFrame: [UIScreen mainScreen].bounds];
_table.delegate = self;
_table.dataSource = self;
[_table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellId"];
[self.view addSubview:_table];
代理回调
#pragma mark - UITableViewDelegate
//点击一行回调
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row) {
case 0:
{
break;
}
default:
break;
}
}
//某section 有多少行
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
//有多少section
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
//行的高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
//sectionHeader高度
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
//返回cell
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//cell的重复利用
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellId"];
cell.textLabel.text = @"content";
return cell;
}
二,使用遇到的问题
1. iOS 15中sectionHeader有个Padding,sectionView上面会多出一块
全局处理一下
if (@available(iOS 15.0, *))
{
[UITableView appearance].sectionHeaderTopPadding = 0;
}
2.关于sectionView
UITableViewStylePlain, // regular table view
UITableViewStyleGrouped, // sections are grouped together
UITableViewStyleInsetGrouped API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos) // grouped
默认情况下 grouped的sectionHeader不会悬浮。plain反之。.plain会出现多余的分割线,grouped不会。(好像可以通过其他方式修改,没试过)
3.间隔线问题
1)设置间隔线
_mTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
类型有以下三种
UITableViewCellSeparatorStyleNone //不显示分割线
UITableViewCellSeparatorStyleSingleLine //单线(默认)(左边不到屏幕)
UITableViewCellSeparatorStyleSingleLineEtched //内嵌线 (左边到屏幕)
2)去掉单个cell 分瞎线
[cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, MAXFLOAT)];
3)去掉多余的间隔线
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
4.cell点击效果
默认点击一个cell后 cell的点击效果会一直存在,要想点击完后,点击效果跟着消失,
在didSelectRowAtIndexPath:方法中
调用[tableView deselectRowAtIndexPath:indexPath animated:NO];
5.cell重叠的现像
如果想隐藏某个cell时把cell的高度设为0.0f的话, 这样会出现 cell重叠的现像。 cell.clipToBounds = YES;解决问题