系统的TableviewCell之间是没有间距的,我们没法改变,那应该怎么来实现呢?
方式1:
通过设置cell的contentView来实现间接,在cell的contentView的顶部或者底部留下一定的间距,这样就会有cell间就有间距的效果。但是这种方式在cell有点击效果的时候,会很明显的看出有分层,因为这时候cell是被点击的,contentView都会有系统点击的阴影效果。这种方式在cell左滑删除,置顶等操作的时候,左滑出的视图会高出一部分(左滑显示出的高度=(cell的高度-留下的间距高度)+ 留下的间距高度[我们不需要的])

方式2:
通过分组的方式间接的实现,每组的Header可以当做是cell之间的间距,每组中只有一个cell(数据显示也会比较简单的)。废话不多说上代码!
#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}


但是呢,这还是会出现一个问题,因为系统默认分组的时候每组的Header会停留在tableview的顶部,这要怎么处理呢?
//去掉UItableview headerview黏性(sticky)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.tableView)
{
CGFloat sectionHeaderHeight = 10; //sectionHeaderHeight
if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}
}

网友评论
再实现下面的方法就可以了啊
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 0.1
}
return 10
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.1
}