iOS UITableViewCell分割线设置

除掉UITableView底部多余行及分割线:

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

隐藏所有的分割线:

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

分割线颜色:

self.tableview.separatorColor = [UIColor redColor];

分割线边距:

  • 方案1 - cellForRowAtIndexPath代理
//定制表格单元分割线
 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellIdentifier];

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 25, 0, 0)];
    }
    return cell;
}
  • 方案2 - willDisplayCell代理
//定制表格单元分割线
  -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 25, 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 25, 0, 0)];
    }
}
  • 方案3 - 对tableView进行设置
 -(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.tableView.tableFooterView = [UIView new];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,15,0,0)];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    [self.tableView reloadData];
}

你可能感兴趣的:(iOS UITableViewCell分割线设置)