iOS开发之有间距的UITableViewCell

Posted 我来看烟花

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS开发之有间距的UITableViewCell相关的知识,希望对你有一定的参考价值。

UITableView是最常用的一个iOS控件,现要做一个如下图的UITableView,其中白色部分就是cell,可是默认的UITableView中cell之间是没有间隔的,那么办呢?网上有2种做法,我这里顺带提一下吧


效果图.png

1、方式一
通过设置cell的contentView来间接实现,在cell的contentView的顶部或者底部留下一定的间距,这样就会有cell间就有间距的效果。但是这种方式在cell有点击效果的时候,会很明显的看出有分层,因为这时候cell是被点击的,contentView都会有系统点击的阴影效果。这种方式在cell左滑删除,置顶等操作的时候,左滑出的视图会高出一部分(左滑显示出的高度=(cell的高度-留下的间距高度)+ 留下的间距高度),很显然这种方式有致命缺陷。**

2、方式二
通过分组的方式间接的实现,每组的Header可以当做是cell之间的间距,每组中只有一个cell,代码如下:

- (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的顶部,这要怎么处理呢?网上也有一种解决办法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView     
if (scrollView == self.tableView)            
CGFloat sectionHeaderHeight = 10;    
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);        
          
   

但是这种方式是通过scroll偏移量来监听和改变tableview的contentInset。

补充:上面的代码只能设置headerView,如果想footerView也没有粘性,怎么办?看到国外一位大神写的如下代码

-(void)scrollViewDidScroll:(UIScrollView *)scrollView 
    if (scrollView == self.tableView)
        
        UITableView *tableview = (UITableView *)scrollView;
        CGFloat sectionHeaderHeight = 64;
        CGFloat sectionFooterHeight = 120;
        CGFloat offsetY = tableview.contentOffset.y;
        if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
        
            tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -sectionFooterHeight, 0);
        else if (offsetY >= sectionHeaderHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight)
        
            tableview.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionFooterHeight, 0);
        else if (offsetY >= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height)         
            tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -(tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight), 0);
        
    

优雅的方式
其实优雅的也是最简单的方法是直接让UITableView中的cell收缩一点,这样UITableView的底色就是分割线的颜色了,如上图就是橘色。这种方式只需要重写cell的setFrame方法即可

-(void)setFrame:(CGRect)frame

    frame.origin.x = 10;//这里间距为10,可以根据自己的情况调整
    frame.size.width -= 2 * frame.origin.x;
    frame.size.height -= 2 * frame.origin.x;
    [super setFrame:frame];   

如果此时想要实现圆角也很简单,直接加上

self.layer.masksToBounds = YES;
self.layer.cornerRadius = 8.0;

此时效果图:


圆角矩形cell.png

PS:这种方式不适合有编辑的情况,因为在编辑的时候会不停调用setFrame方法,导致错乱,此时建议使用上面的第二种方案。感谢简友的提醒,之前做的是无编辑的情况,有编辑的没有测试。



以上是关于iOS开发之有间距的UITableViewCell的主要内容,如果未能解决你的问题,请参考以下文章

iOS UITableViewCell 间距设置

UITableViewCell 在 iOS 7 中左右有额外的间距

UITableViewCell 之间的间距是多少?

UITableviewcell单元格间距

李洪强iOS开发之有参方法的声明实现和调用

iOS9 UITableViewCell 分割线左顶头