【问题标题】:Custom rounded corners for first and last cell in a grouped UITableView分组 UITableView 中第一个和最后一个单元格的自定义圆角
【发布时间】:2012-09-11 14:43:48
【问题描述】:

我的 iPhone 应用目前正在显示一个包含 2 个部分的分组表。我尝试使用以下方法更改每个部分的第一个和最后一个单元格(或在只有一行的部分的同一单元格上)的圆角半径:

cell.layer.cornerRadius = 4;

或作用于整个tableview:

self.tableView.layer.cornerRadius = 4;

但没有运气...当然,在执行此操作之前,我已经导入了 QuartzCore。看起来只有当表格以普通样式显示时才能自定义角。

理想的解决方案是自定义第一个角的顶角和最后一个角的底角。

有什么建议吗?

【问题讨论】:

  • 分组表每组的角是圆角仪式吗?
  • 我也遇到过同样的问题...而且我不认为分组表格在你想编辑它的形状时很有效
  • 看看这个;可能有帮助:stackoverflow.com/questions/1106861/…
  • 不分组 tableView 本身有圆角??

标签: iphone rounded-corners uitableview cornerradius


【解决方案1】:

您可以在单元格的背景上添加UIView,使单元格背景为 clearcolor 。将UIView 调整为圆角。

【讨论】:

    【解决方案2】:
    UIView  *view = [UIView alloc]initWithFrame:YourFrame];
    view.layer.cornerRadius = 8;
    
    cell.backgroundView = view;
    

    不要忘记UITableView 背景颜色是透明的。

    【讨论】:

    • 如果只有一行就可以了。但是对于多行,唯一要修改的圆角应该是第一个的顶部和最后一个的底部。我认为这种方法也会影响单元格之间的所有角落。
    • - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0 || indexPath.row == [ tableView numberOfRowsInSection:indexPath.section]-1) { UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 44)]; view.layer.cornerRadius = 4; view.backgroundColor = [UIColor redColor]; cell.backgroundView = 视图; //cell.backgroundColor = [UIColor clearColor]; } }
    【解决方案3】:

    最简单的方法是:

    1. 在 xib 文件中创建自定义单元格,设置其唯一标识符,如:@"beginCell", @"middleCell", @"endCell"。可以按照本教程制作:http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder/

    2. 在方法中 - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 您必须找到您在该部分的中间或开头/结尾处,并使用具有正确标识符的单元格。

    【讨论】:

    • 您也可以像以前一样执行自定义单元格绘制,但在 tableView:cellForRowAtIndexPath: 方法中进行。同样,您需要为中间单元格和开始/结束单元格设置单独的标识符。
    【解决方案4】:

    如果您只需要圆角,还有另一种解决方案(受本地 uitableview(cell) 的分组样式启发,在 slowmo 中 :))。应该是这样的:

    细胞子类...

    @interface MyCell : UITableViewCell
    @property (nonatomic) top;
    @property (nonatomic) bottom;
    @end
    
    @implementation MyCell
    -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            self.backgroundColor = [UIColor clearColor]; // remove white default background
            //example settings, UIImageViews with resizable images can be used too
            self.backgroundView = [[UIView alloc] init];
            self.selectedBackgroundView = [[UIView alloc] init];
            self.backgroundView.backgroundColor = [UIColor greenColor];
            self.selectedBackgroundView.backgroundColor = [UIColor blueColor];
            self.backgroundView.layer.cornerRadius = self.selectedBackgroundView.layer.cornerRadius = 5; // 
        }
        return self;
    }
    -(void)layoutSubviews{
        [super layoutSubviews];
        self.backgroundView.frame = self.selectedBackgroundView.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(self.top?0:-self.backgroundView.layer.cornerRadius, 0, self.bottom?0:-self.backgroundView.layer.cornerRadius, 0));
    }
    -(void)setTop:(BOOL)top{
        _top = top;
        [self setNeedsLayout];
    }
    -(void)setBottom:(BOOL)bottom{
        _bottom = bottom;
        [self setNeedsLayout];
    }
    @end
    

    在数据源中你可以这样做:...

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
          ...cell initialization stuff...
          [(MyCell *)cell setTop:(indexPath.row == 0)];
          [(MyCell *)cell setBottom:(indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)];
    
          return cell;
    }
    

    优点:

    • 所有单元格的背景
    • 易于使用
    • 动画(您正在更改位置而不是图像)
    • 也可以用于普通样式(但要注意部分:))

    缺点:

    • 需要单元格的子类(即使更改了 rowHeight,我也找不到调整背景位置的方法)
    • 不能解决我的整个部分的圆角问题(您必须手动为截面视图圆角)

    我希望这对某人有所帮助,但请注意,此代码未经测试!我在发布这个答案前几分钟就实现了这个想法,它似乎有效:)

    【讨论】:

      【解决方案5】:

      以下是 Swift 4.2 中的工作原理:

      func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
      {
          let cornerRadius = 5
          var corners: UIRectCorner = []
      
          if indexPath.row == 0
          {
              corners.update(with: .topLeft)
              corners.update(with: .topRight)
          }
      
          if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1
          {
              corners.update(with: .bottomLeft)
              corners.update(with: .bottomRight)
          }
      
          let maskLayer = CAShapeLayer()
          maskLayer.path = UIBezierPath(roundedRect: cell.bounds,
                                        byRoundingCorners: corners,
                                        cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)).cgPath
          cell.layer.mask = maskLayer
      }
      

      【讨论】:

      • raywenderlich.com/… : "注意:使用 Core Graphics 绘制静态视图通常足够快,但如果您的视图四处移动或需要频繁重绘,则应使用 Core Animation 层。Core Animation 已优化,因此处理大部分处理的是 GPU,而不是 CPU。相比之下,CPU 处理由 Core Graphics 在 draw(_:)" 中执行的视图绘制"
      • @rs7 似乎这使事情变得更加复杂,并且认为在性能方面没有必要。但是,请发布您的答案并联系我;我很想看看结果如何。
      • 这是完美的答案。
      【解决方案6】:

      对于故事板,iOS >= 13.0

      将 UITableView 样式设置为Inset Grouped

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多