OC_实现UITableviewCell之间的间距

来源:互联网 发布:嵌入式linux启动 编辑:程序博客网 时间:2024/05/16 19:52

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

方式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;}

图片2

每组的Header会停留在tableview的顶部.gif

但是呢,这还是会出现一个问题,因为系统默认分组的时候每组的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);        }    }}

取消UItableview headerview黏性

0 0
原创粉丝点击