在UITableView实现图片上面的效果,百度一下看了别人的实现方案有下面2种:

1.UITableView section里面嵌套UITableView然后在上面实现圆角和阴影,  弊端代码超多我看了下就不想看了立马放弃.

2.UICollectionView 实现,  但是我原来的UI是UITableView写的就懒得重写.

找来找去都没一种简单的实现方案,自己有事了几个绘图API,没有达到图片的效果.想了两天灵光一闪,一个超简单的方法就能实现section+圆角+阴影 .  分享出来给有需要的人

UITableViewCell 是系统的没有自定义,直接在上面插入一个UIView做阴影图层就能达到效果,原理就是利用clipsToBounds属性UIView的layer图层超出view的部分不显示

贴代码

  1. //============阴影层===========
  2.  
  3. @interface SubCellShadowView :UIView
  4.  
  5. @property (nonatomic, strong) CAShapeLayer *shadowLayer;
  6.  
  7. @property (nonatomic, strong) CALayer *separatorLine;
  8.  
  9. @end
  10.  
  11. @implementation SubCellShadowView
  12.  
  13. @end
  1. //===============Cell==================
  2.  
  3. @interface SubCell : UITableViewCell
  4.  
  5. @property (nonatomic, strong) SubCellShadowView *bgView;
  6.  
  7. @property (nonatomic, strong) NSIndexPath *indexPath;
  8.  
  9. @property (nonatomic) NSInteger rowInSection;//每一组的行数
  10.  
  11. @end
  12.  
  13. @implementation SubCell
  14.  
  15. - (void)awakeFromNib {
  16.  
  17. [super awakeFromNib];
  18.  
  19. self.selectionStyle = UITableViewCellSelectionStyleNone;
  20.  
  21. self.clipsToBounds = NO;
  22.  
  23. SubCellShadowView *bgView = [[SubCellShadowView alloc] init];
  24.  
  25. [self insertSubview:bgView atIndex:];
  26.  
  27. self.bgView= bgView;
  28.  
  29. CAShapeLayer *shadow = [CAShapeLayer layer];
  30.  
  31. shadow.shadowColor = [UIColor blackColor].CGColor;
  32.  
  33. shadow.shadowOffset=CGSizeMake(,);
  34.  
  35. shadow.shadowOpacity=0.15;
  36.  
  37. [bgView.layeraddSublayer:shadow];
  38.  
  39. bgView.shadowLayer= shadow;
  40.  
  41. CALayer*line = [CALayerlayer];
  42.  
  43. line.backgroundColor = [UIColor groupTableViewBackgroundColor].CGColor;
  44.  
  45. [bgView.layeraddSublayer:line];
  46.  
  47. bgView.separatorLine= line;
  48.  
  49. }
  50.  
  51. -(void)layoutSubviews{
  52.  
  53. [super layoutSubviews];
  54.  
  55. UIBezierPath*bgBezierPath =nil;
  56.  
  57. CGFloat cornerRaduis =7.0;//觉得阴影大的可以把半径调小,半径大的话阴影面积会变大
  58.  
  59. if(self.indexPath.row== && self.rowInSection==) {//单组单行
  60.  
  61. self.bgView.clipsToBounds=NO;
  62.  
  63. self.bgView.frame=self.bounds;
  64.  
  65. CGRect rect = UIEdgeInsetsInsetRect(self.bgView.bounds, UIEdgeInsetsMake(, , , ));
  66.  
  67. bgBezierPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRaduis, cornerRaduis)];
  68.  
  69. }elseif(self.indexPath.row==) {// 第一行
  70.  
  71. self.bgView.clipsToBounds=YES;
  72.  
  73. self.bgView.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(-, , , ));
  74.  
  75. CGRect rect = UIEdgeInsetsInsetRect(self.bgView.bounds, UIEdgeInsetsMake(, , , ));
  76.  
  77. bgBezierPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(cornerRaduis, cornerRaduis)];
  78.  
  79. }elseif(self.indexPath.row==self.rowInSection-) {// 最后一行
  80.  
  81. self.bgView.clipsToBounds=YES;
  82.  
  83. self.bgView.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(, , -, ));
  84.  
  85. CGRect rect = UIEdgeInsetsInsetRect(self.bgView.bounds, UIEdgeInsetsMake(, , , ));
  86.  
  87. bgBezierPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(cornerRaduis, cornerRaduis)];
  88.  
  89. }else{// 中间行
  90.  
  91. self.bgView.clipsToBounds=YES;
  92.  
  93. self.bgView.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(, , , ));
  94.  
  95. CGRect rect = UIEdgeInsetsInsetRect(self.bgView.bounds, UIEdgeInsetsMake(, , , ));
  96.  
  97. bgBezierPath = [UIBezierPathbezierPathWithRect:rect];
  98.  
  99. }
  100.  
  101. self.bgView.shadowLayer.path= bgBezierPath.CGPath;
  102.  
  103. self.bgView.shadowLayer.shadowPath= bgBezierPath.CGPath;
  104.  
  105. self.bgView.shadowLayer.fillColor = [UIColor whiteColor].CGColor;
  106.  
  107. //分割线 非单组单行 非最后一行
  108.  
  109. if(!(self.indexPath.row==&&self.rowInSection==) && !(self.indexPath.row==self.rowInSection-)) {
  110.  
  111. self.bgView.separatorLine.frame = CGRectMake(self.bgView.frame.origin.x+, self.bgView.frame.size.height-, self.bgView.frame.size.width-*, 1.0);
  112.  
  113. }
  114.  
  115. }
  116.  
  117. @end

UITableView section 圆角 阴影的更多相关文章

  1. IOS UIView圆角,阴影,边框,渐增光泽

    圆角 sampleView.layer.cornerRadius = 2.5; // 圓角的弧度sampleView.layer.masksToBounds = YES; 阴影 sampleView. ...

  2. UWP Button添加圆角阴影(三)

    原文:UWP Button添加圆角阴影(三) Composition DropShadow是CompositionAPI中的东西,使用Storyboard设置某个属性,就是频繁的触发put_xxx() ...

  3. UWP Button添加圆角阴影(二)

    原文:UWP Button添加圆角阴影(二) 阴影 对于阴影呢,WindowsCommunityToolkit中已经有封装好的DropShadowPanel啦,只要引用Microsoft.Toolki ...

  4. UWP Button添加圆角阴影(一)

    原文:UWP Button添加圆角阴影(一) 众所周知,17763之前的UWP控件,大部分是没有圆角属性的:而阴影也只有17763中的ThemeShadow可以直接在xaml中使用,之前的版本只能用D ...

  5. CSS3圆角,阴影,透明

    CSS实现圆角,阴影,透明的方法很多,传统的方法都比较复杂,用CSS3就方便很多了,虽然现在各浏览器对CSS3的支持还不是很好,但不久的将来CSS3就会普及. 1.圆角 CSS3实现圆角有两种方法. ...

  6. Qt之圆角阴影边框

    Qt的主窗体要做出类似WIN7那种圆角阴影边框,这一直是美工的需求. 这里是有一些门道的,尤其是,这里藏着一个很大的秘密. 这个秘密是一个QT的至少横跨3个版本,存在了2年多的BUG... https ...

  7. 让group tableview每个section拥有阴影和圆角

    #import <UIKit/UIKit.h> @class GroupShadowTableView; @protocol GroupShadowTableViewDelegate &l ...

  8. iOS tableView Section圆角方案

    给tableView的section设置圆角 首先给让cell左右偏移一点的距离,通过重写cell的setframe方法来实现 -(void)setFrame:(CGRect)frame{ CGFlo ...

  9. 设置UITableView section间距

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0 ...

随机推荐

  1. springboot缓存注解——@Cacheable

    @Cacheable: 1,方法运行之前,先查询Cache(缓存组件),按照cacheName指定的名字获取(CacheManager获取相应缓存) 第一次获取缓存如果没有Cache组件会自会自动创建 ...

  2. Web API学习笔记(Python实现)

    参考指南: Web API入门指南 http://www.cnblogs.com/guyun/p/4589115.html 用Python写一个简单的Web框架 http://www.cnblogs. ...

  3. Facebook主页照片和封面照片的尺寸要求

    为什么好好的照片上传到Facebook后效果总不理想?为了避免你的照片在上传时被压缩,建议你尽量调整一下图片大小和格式,下面一起来看看Facebook主页照片和封面照片的尺寸要求. 1. Facebo ...

  4. 【转载】Linux常用命令

    Linux常用命令大全(非常全!!!) 转载出处:https://www.cnblogs.com/yjd_hycf_space/p/7730690.html 系统信息 arch 显示机器的处理器架构( ...

  5. JS数组遍历

    1. forEach() 循环数组,不会改变元素,不会返回新数组 arr.foreach((value,index)=>{}) 2. map() 遍历数组,对每个元素进行处理,之后返回元素:会返 ...

  6. git相关操作记录

    拉取远端并强制覆盖本地文件 有时候会出现想放弃本地修改的情况,那么只需要下面几行命令 git fetch --all git reset --hard origin/master git pull / ...

  7. AutoLayout的坑

    本文投稿文章,作者:MangoMade(简书) AutoLayout非常强大也非常易用,可读性也很强,加上各种第三方AutoLayout库,让你布起局来犹如绷掉链子的狗!根本停不下来!以前的 1 la ...

  8. mysql权限参考

    mysql日常管理和应用工作中,大家经常会涉及到授权问题,下面,我们就列举下权限相关的参考. 1.管理权限(Administrative Privileges) Privilege Name      ...

  9. WebService调用SSAS教程

    WebService调用SSAS教程 一.创建SSAS项目 使用SQL Server Business Intelligence Development Studio新建Analysis Servic ...

  10. day_45_Django

    day45 内容回顾 #### 1. HTTP协议 tcp/IP协议 HTTP特性: 无连接 请求--响应模式 请求格式 响应格式 Django相关 Django下载 命令行: pip install ...