iOS tableview 设置 组section 间距

iOS tableView style 设置为 组样式的时候,间距一般都不是我们想要的

_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

section 默认间距

我们一般想要的是比较小的间距,用 cell 来代替、或者 cell 用个 view 加个分隔条都可以实现分割效果,其实 section 的间距是 用 sectionHeader 高度 和 sectionFooter 高度 的和决定的,比如sectionHeader 的高度是 5,sectionFooter 的高度是 5,那么组间距就是 10。

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 8;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0;

}

这样子就可以设置 组间距为指定的高度了,iOS 11 还要实现 下面两个代理方法

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return [UIView new];
}

猜你喜欢

转载自blog.csdn.net/csdn2314/article/details/86626929