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

tableView圆角边框解决方案

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

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

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

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

  • 设置过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.row0和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

iOS开发小技巧 -- tableView-section圆角边框解决方案的更多相关文章

  1. iOS开发小技巧--TableView Group样式中控制每个section之间的距离

    一.TableView的Group样式中,默认的每个section都有sectionHeader和sectionFooter,只要调整这两个的大小就可以实现section之前的间距扩大或缩小 二.项目 ...

  2. iOS开发小技巧--图片的圆角处理

    图片圆角处理方案一: 缺点是图片多了感觉卡 设置图层的CornerRadius属性 -- 设置了圆角后,运行程序没有反应,多半是没有设置下面这句:让内容遵循设置的边缘 masksToBounds = ...

  3. iOS开发小技巧--TableView中headerView的循环利用,以及自定义的headerView

    一.首先要搞清楚,tableView中有两种headerView,一个是tableHeaderView,另一个是headerView.前者就一个;后者根据session决定个数 headerView的 ...

  4. iOS开发小技巧--设置按钮圆角

    方法一:代码设置 方法二:通过图形化界面

  5. iOS开发小技巧--tableView中实现无数据无分割线,有数据才有分割线

    通过通讯录练习GET的技能

  6. iOS开发小技巧 - UILabel添加中划线

    iOS开发小技巧 遇到的问题: 给Label添加中划线,然后并没有效果 NSString *str = [NSString stringWithFormat:@"合计金额 ¥%.2f&quo ...

  7. iOS开发小技巧 - runtime适配字体

    iOS开发小技巧 - runtime适配字体 版权声明:本文为博主原创文章,未经博主允许不得转载,有问题可联系博主Email: liuyongjiesail@icloud.com 一个iOS开发项目无 ...

  8. iOS开发小技巧--即时通讯项目:消息发送框(UITextView)高度的变化; 以及UITextView光标复位的小技巧

    1.即时通讯项目中输入框(UITextView)跟随输入文字的增多,高度变化的实现 最主要的方法就是监听UITextView的文字变化的方法- (void)textViewDidChange:(UIT ...

  9. ios开发小技巧之提示音播放与震动

    在ios开发中,有时候我们需要频繁播放某种提示声音,比如微博刷新提示音.QQ消息提示音等,对于这些短小且需要频繁播放的音频,最好将其加入到系统声音(system sound)里. 注意: 需要播放的音 ...

随机推荐

  1. VS2015 Update2中有关cordova和xamarin安装的问题

    最近VS2015出了Update2,当然是第一时间进行了安装,中间过程曲折,反复安装卸载n次,也算是获得了一定的安装经验值.现在说一下经常出的问题. Update2里最吸引人的当然是跨平台开发的部分, ...

  2. querystring模块

    querystring处理参数的小利器. 下面是querystring的四个方法.   ①stringify:将一个参数对象序列化为一个字符串 eg: querystring.stringify({n ...

  3. Java web.xml 配置详解

    在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...

  4. push方法的兼容性问题

    在IE8及以下中,不支持aplly方法中的第二个参数是 伪数组 需要对push方法进行封装. 将 push 的判断,放入一个沙箱中: 好处:在页面加载的时候就会执行这段代码,保证了代码只会检测一次 以 ...

  5. 封装 用canvas绘制直线的函数--面向对象

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. SharePoint文档库文件夹特殊字符转义

    当我们在SharePoint网站文档库中新建文件夹时包含了~ " # % & * : < > ? / \ { | }字符时(一共15个), 或者以.开头或者结束,或者包含 ...

  7. yii2权限控制rbac之rule详细讲解

    作者:白狼 出处:http://www.manks.top/yii2_rbac_rule.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留 ...

  8. Eclipse使用Maven创建web3.0项目

    安装Maven插件 这一步不细说了,自己下载的Eclipse-JAVA EE 版已自带 Maven插件 开始创建 文本1New一个 Maven Web App项目:File-->New--> ...

  9. 织梦Dedecms安全设置

    织梦DedeCMS是一款非常流行的CMS,很多刚开始建站人都用的织梦,一方面是织梦比较容易操作;另一方面是织梦的SEO方面做的确实比其他的系统要好一些.这些都导致织梦的用户群是非常庞大的,用的人多了, ...

  10. ORACLE分区表梳理系列(一)- 分区表概述、分类、使用方法及注意事项

    版权声明:本文发布于http://www.cnblogs.com/yumiko/,版权由Yumiko_sunny所有,欢迎转载.转载时,请在文章明显位置注明原文链接.若在未经作者同意的情况下,将本文内 ...