为分组UITableView中的第一个和最后一个单元格定制圆角

问题描述 投票:1回答:5

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

cell.layer.cornerRadius = 4;

或者对整个桌面视图采取行动:

self.tableView.layer.cornerRadius = 4;

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

理想的解决方案是在最后一个中定制顶角和底角。

有什么建议?

iphone rounded-corners uitableview cornerradius
5个回答
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中,您必须找到您在该部分的中间或开始/结束处并使用具有适当标识符的单元格。

2
投票

你可以做的是在细胞的背景上添加一个UIView,使细胞背景为清晰的颜色。调整UIView有圆角。


2
投票

这是在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
}

1
投票
UIView  *view = [UIView alloc]initWithFrame:YourFrame];
view.layer.cornerRadius = 8;

cell.backgroundView = view;

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


1
投票

如果你只需要圆角,那就是另一种解决方案(灵感来自本机uitableview(单元格)的分组样式,在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

在dataSource中你可以这样做:...

- (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;
}

优点:

  • 所有细胞的一个背景
  • 使用方便
  • animateable(你改变位置而不是图像)
  • 也可以用于普通风格(但注意部分:))

缺点:

  • 需要单元格的子类(即使在更改rowHeight后我找不到调整背景位置的方法)
  • 没有解决我的问题与整个部分的圆角(你必须圆角为剖面视图manualy)

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

© www.soinside.com 2019 - 2024. All rights reserved.