正方形而不是UITableViewCell分组样式中的圆角

问题描述:

我想为我的分组tableview单元格而不是默认的圆角方形角,我不只是想用图像来给出这种效果。可能吗?正方形而不是UITableViewCell分组样式中的圆角

+1

为什么不自定义UITableviewStylePlain?.. – Mat

最简单地说,在你tableView:cellForRowAtIndexPath:使用

cell.backgroundView = [[[UIView alloc] initWithFrame:cell.bounds] autorelease]; 
+3

忽略ARC域中的autorelease调用。 – mbehan

您可以将UITableViewCell的backgroundViewselectedBackgroundView设置为您自己创建的自定义UIView。这应该给你一个方格。

接受的答案的效果很好,但不幸的是消除细胞间的分隔线。如果你有一个3x3像素TableCellBackground.png,与白色像素的顶部两行和最低的第三排灰(相匹配的分隔符颜色),你可以这样做:

// To square the corners, we replace the background view of the top and bottom cells. 
    // In addition, the top cell needs a separator, which we get from TableCellBackground.png. 
    UIImage *stretchableImage = [UIImage imageNamed:@"TableCellBackground.png"]; 
    UIImage *cellImage = [stretchableImage resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 1, 1)]; 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.bounds]; 
    imageView.image = cellImage; 
    cell.backgroundView = imageView; 

首先设置你的背景视图然后设置单元格的选定背景视图(与单元格背景相同的边界)

您可以指定cornerRadius属性,以便您可以根据需要设置圆角的圆角,我省略了该属性

以下是两者的代码:

 UIView *bg = [[UIView alloc] initWithFrame:cell.bounds]; 
     bg.backgroundColor = [UIColor colorWithRed:0.980 green:0.988 blue:0.984 alpha:1]; 
     bg.layer.borderColor = [UIColor colorWithRed:0.827 green:0.827 blue:0.835 alpha:1].CGColor; 
     bg.layer.borderWidth = kCellBorderWidth; 
//  bg.layer.cornerRadius= kCellBorderRadius; 
     cell.backgroundView = bg; 

     // to make cell selection square and not round (which is by default) 
     UIView *bg_selected = [[UIView alloc] initWithFrame:cell.bounds]; 
     bg_selected.backgroundColor = [UIColor lightGrayColor]; 
     bg_selected.layer.borderColor = [UIColor colorWithRed:0.827 green:0.827 blue:0.835 alpha:1].CGColor; 
     bg_selected.layer.borderWidth = kCellBorderWidth; 
     cell.selectedBackgroundView = bg_selected;