UITableView分割线顶头

当UITableView的style设置为UITableViewStylePlain时,最后一个Cell的分割线没有顶头。作为一个处女座,强迫症又犯了,Stack overflowe上找到了解决方法,拿出小本本记一下。

方法一:将TableView的类型设置为UITableViewStyleGrouped

这样每个分区的第一行和最后一行分割线是顶头的。
UITableViewStyleGrouped

方法二:在代理方法中修改分割线的约束

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == ([tableView numberOfRowsInSection:indexPath.section] - 1)) {
// Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
// Prevent the cell from inheriting the Table Views margin settings
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
// Explictly set your cell's layout margins
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
}

这里我只添加了最后一个cell的分割线。

方法三:设置cell的separatorInset属性

通过设置cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);可以将分割线顶头。但是当设置left为正数时,整个contentView内控件都会发生偏移,个人认为可以只用作设置分割线顶头来用。