博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发小技巧 -- tableView-section圆角边框解决方案
阅读量:7026 次
发布时间:2019-06-28

本文共 3125 字,大约阅读时间需要 10 分钟。

【iOS开发】tableView-section圆角边框解决方案

tableView圆角边框解决方案

  • iOS 7之前,图下圆角边框很容易设置

  • iOS 7之后,tableviewcell的风格不再是圆角了

设置tableView中section圆角边框,需求如下:

762322-20161116103127654-1033719376.png

找了很多办法都没办法解决。

  • 设置过tableView的边框,但是发现,滑动tableView的时候,其实是里面的content在移动,也就是,向上向下滑,边框并不会动=。=。

  • 可以试着打印tableView的frame,发现frame的x.y并不会移动。

最后解决了,找了两种方案

【方案一】- 自定义cell

圆角边框为UIImage,充当自定义cell的背景图.

  • 1.方法比较简单,就不粘代码了,弄上边圆角,下边圆角,没有圆角,上下边都有圆角四张图片。

  • 2.判断加载的是section中的indexpath.row是几,对应加载哪种圆角图片,就可以了。

【方法二】- cell重绘

给section,绘制边框。

  • 1.在UITableViewDelegate有个展示cell的代理方法,在展示cell的时候,然后为section中的cell绘制边框

  • 2.index path.row==0和index path.row==最后一个 的时候,加圆角。

  • 3.此方法,可以直接复制粘贴使用,另外,如果此需求用到的地方比较多,可以构建基类或者写在category里面,方便使用代码如下:

#pragma mark - Table view data source// 重新绘制cell边框- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ if ([cell respondsToSelector:@selector(tintColor)]) { // if (tableView == self.tableView) { CGFloat cornerRadius = 10.f; cell.backgroundColor = UIColor.clearColor; CAShapeLayer *layer = [[CAShapeLayer alloc] init]; CGMutablePathRef pathRef = CGPathCreateMutable(); CGRect bounds = CGRectInset(cell.bounds, 10, 0); BOOL addLine = NO; if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) { CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius); } else if (indexPath.row == 0) { CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds)); CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius); CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius); CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds)); addLine = YES; } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) { CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds)); CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius); CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius); CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds)); } else { CGPathAddRect(pathRef, nil, bounds); addLine = YES; } layer.path = pathRef; CFRelease(pathRef); //颜色修改 layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.5f].CGColor; layer.strokeColor=[UIColor whiteColor].CGColor; if (addLine == YES) { CALayer *lineLayer = [[CALayer alloc] init]; CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale); lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight); lineLayer.backgroundColor = tableView.separatorColor.CGColor; [layer addSublayer:lineLayer]; } UIView *testView = [[UIView alloc] initWithFrame:bounds]; [testView.layer insertSublayer:layer atIndex:0]; testView.backgroundColor = UIColor.clearColor; cell.backgroundView = testView; } // }}

来源http://www.jianshu.com/p/da1adec5a601

转载地址:http://spoxl.baihongyu.com/

你可能感兴趣的文章
队列 句子分析 精辟的诠释 有图片
查看>>
在switch的default代码块中增加AssertionError错误
查看>>
JS:1.3,函数(function)
查看>>
Ubuntu下升级Git以及获取ssh keys的代码
查看>>
在C#代码中应用Log4Net(一)简单使用Log4Net
查看>>
webservice 测试窗体只能用于来自本地计算机的请求
查看>>
让WordPress主题支持语言本地化(使用poedit软件实现中文翻译功能)
查看>>
[WinAPI] API 6 [操作驱动器挂载点]
查看>>
SQL 在查询中插入行号--自定义分页的另外一种实现方式
查看>>
另类Unity热更新大法:代码注入式补丁热更新
查看>>
(cljs/run-at (JSVM. :browser) "搭建刚好可用的开发环境!")
查看>>
有时我们需要调用一个函数时,返回多个不同类型的数据
查看>>
IdentityServer4 通过 AccessToken 获取 UserClaims
查看>>
HIVE json格式数据的处理
查看>>
淘宝API开发系列---阿里.聚石塔.开放平台的使用
查看>>
I.MX6 KEY_ROW4 can't as GPIO pin
查看>>
[大数据之Spark]——Actions算子操作入门实例
查看>>
5.4. package / compress and decompress
查看>>
Vuejs——(7)过渡(动画)
查看>>
(二十一)java字符串替换的问题
查看>>