使用自定义单元格/自定义背景图像分组UITableView

时间:2012-10-16 19:08:58

标签: ios uitableview

我有一个自定义的UITableViewCell。 xib文件如下所示:

enter image description here

可以通过选择来打开和关闭单元格。当单元格关闭时,只有标题标签和顶部背景图像可见。这是表格视图在模拟器上的样子,打开和关闭:

enter image description here enter image description here

我想弄清楚如何处理圆角。目前,我正在检查单元格是第一个,最后一个还是中间单元格,并应用掩码:

if (row == 0)
{
    // Create the path (with only the top corners rounded)
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.titleBackgroundImageView.bounds
                                                   byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
                                                         cornerRadii:CGSizeMake(10.0, 10.0)];

    // Create the shape layer and set its path
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = cell.titleBackgroundImageView.bounds;
    maskLayer.path = maskPath.CGPath;

    // Set the newly created shape layer as the mask for the image view's layer
    cell.titleBackgroundImageView.layer.mask = maskLayer;
    cell.bodyBackgroundImageView.layer.mask = nil;
}
else if ((row + 1) == [itemsForSection count])
{
    // Create the path (with only the bottom corners rounded)
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.titleBackgroundImageView.bounds
                                                   byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
                                                         cornerRadii:CGSizeMake(10.0, 10.0)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];

    // Create the shape layer and set its path
    BOOL isOpened = [[[self.cellOpenedStatusMutableDictionary objectForKey:sectionTitle] objectAtIndex:row] boolValue];
    if (isOpened)
    {
        cell.titleBackgroundImageView.layer.mask = nil;
    }
    else
    {
        maskLayer.frame = cell.titleBackgroundImageView.bounds;
        maskLayer.path = maskPath.CGPath;        
        cell.titleBackgroundImageView.layer.mask = maskLayer;
    }
        // Create the path (with only the bottom corners rounded)
    maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.bodyBackgroundImageView.bounds
                                     byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
                                           cornerRadii:CGSizeMake(10.0, 10.0)];

    // Create the shape layer and set its path
    maskLayer = [CAShapeLayer layer];
    maskLayer.frame = cell.titleBackgroundImageView.bounds;
    maskLayer.path = maskPath.CGPath;

    cell.bodyBackgroundImageView.layer.mask = maskLayer;
}
else
{
    cell.titleBackgroundImageView.layer.mask = nil;
    cell.bodyBackgroundImageView.layer.mask = nil;
}

但正如您所看到的,它对底部单元格不起作用 - 表格视图的边框模糊不清。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

看起来您只需要将表格视图的分隔符样式设置为“单行”(UITableViewCellSeparatorStyleSingleLine)而不是“单行蚀刻”。