我在 Interface Builder 中设置了一个 TableView ,分隔符设置为单行,在数据源类中我有以下代码:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
UIImageView *topCellBackgroundImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"grouped-cell-bg-top"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)]];
cell.backgroundView = topCellBackgroundImageView;
}
// If it's the last row
else if (indexPath.row == ([tableView numberOfRowsInSection:0] - 1)) {
UIImageView *bottomCellBackgroundImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"grouped-cell-bg-bottom"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)]];
cell.backgroundView = bottomCellBackgroundImageView;
}
else {
UIImageView *middleCellBackgroundImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"grouped-cell-bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(3.0, 3.0, 3.0, 3.0)]];
cell.backgroundView = middleCellBackgroundImageView;
}
}
在 viewDidLoad
中,我这样做:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorColor = [UIColor redColor];
self.tableView.backgroundView = nil;
self.tableView.backgroundColor = [UIColor colorWithRed:245/255.0 green:244/255.0 blue:240/255.0 alpha:1.0];
}
但是边框颜色永远不会出现。我尝试将它们设置在图像中,但显然这会带来很多图像居中问题,并且它们的边界会重叠。
更新:
仔细查看后,它确实得到了设置,但似乎设置单元格的 backgroundView 会将您设置为 backgroundView 的内容覆盖 separatorColor。当我删除 willDisplayCell:
方法时,红色显示正常(当我选择单元格时,即使设置了 backgroundView,它也会显示红色,直到我取消选择它)。问题是,如果我设置了 backgroundView,我该如何设置 separatorColor?
最佳答案
您可以使用的一个技巧是将 UIView
添加到您的背景 View 中,背景颜色为红色,高度为 1 磅。另一种选择是要求您的设计师在背景 View 的图像文件中加入这样一条红线。
关于ios - 自定义 UITableViewCell - 我设置了背景图像,但如何设置边框颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17506069/