0
点赞
收藏
分享

微信扫一扫

iOS 多个分区的tableView设置每个分区第一行和最后一行圆角效果

尤克乔乔 2021-09-19 阅读 77

现在出现很多那种分区的tableView,每个分区的第一行和最后一行是圆角的,中间行没有圆角效果,如下图所示的效果:


有一个比较简单的实现方法:在自定义的cell中创建3个UIView来辅助实现,一个topView,设置背景色为白色,一个bottomView,设置背景色为白色,一个centerView,设置背景色为白色。

    topView = [[UIView alloc] init];
topView.backgroundColor = [UIColor whiteColor];
[self.contentView addSubview:topView];

bottomView = [[UIView alloc] init];
bottomView.backgroundColor = [UIColor whiteColor];
[self.contentView addSubview:bottomView];

centerView = [[UIView alloc] init];
centerView.layer.cornerRadius = 4;
centerView.clipsToBounds = YES;
centerView.backgroundColor = [UIColor whiteColor];
[self.contentView centerView];

将centerView的高度设置的与contentView的高度一致,并在四个边角设置好圆角效果,topView放在最上面,顶部挨着contentView的顶部,高度可以设置小一点,我设置的是10,bottomView放在最下面,底部挨着contentView的底部,高度也设置的是10,还有三个view的创建添加顺序一定是centerView在最上面。
在cell的.h文件公开一个方法来设置圆角效果

- (void)refreshCell:(BOOL)isFirst lastCell:(BOOL)isLast;

在.m文件中实现方法

- (void)refreshCell:(BOOL)isFirst lastCell:(BOOL)isLast {
topView.hidden = isLast;
bottomView.hidden = isFirst;
}

在cell的代理方法cellForRow里,用cell对象调用这个方法
第一行[cell refreshCell:NO lastCell:YES];
最后一行[cell refreshCell:YES lastCell:NO];

举报

相关推荐

0 条评论