首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS开发之有间距的UITableViewCell

iOS开发之有间距的UITableViewCell

作者头像
YungFan
发布2018-05-03 14:40:02
1.2K0
发布2018-05-03 14:40:02
举报
文章被收录于专栏:学海无涯学海无涯

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方法,导致错乱,此时建议使用上面的第二种方案。感谢简友的提醒,之前做的是无编辑的情况,有编辑的没有测试。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.07.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档