【发布时间】:2011-12-03 10:52:46
【问题描述】:
我希望我的分组表格视图单元格使用方角而不是默认的圆角,而且我不只是想使用图像来产生这种效果。有可能吗?
【问题讨论】:
-
为什么不自定义一个 UITableviewStylePlain?..
标签: iphone objective-c uitableview cell tableview
我希望我的分组表格视图单元格使用方角而不是默认的圆角,而且我不只是想使用图像来产生这种效果。有可能吗?
【问题讨论】:
标签: iphone objective-c uitableview cell tableview
最简单的,在你的tableView:cellForRowAtIndexPath: 使用
cell.backgroundView = [[[UIView alloc] initWithFrame:cell.bounds] autorelease];
【讨论】:
您可以将 UITableViewCell 的 backgroundView 和 selectedBackgroundView 设置为您自己创建的自定义 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;
【讨论】: