ios - 自定义 UITableViewCell - 我设置了背景图像,但如何设置边框颜色?

标签 ios objective-c uitableview

我在 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/

相关文章:

ios - DrawText on Image with text 调整到图像的大小

iOS 以编程方式显示基于当前用户的 View Controller

objective-c - 尝试从 UITableView 中删除单元格时出错

ios - 将 UINavigationBar 附加到 UIScrollView 以获得缩小的标题

ios - 使用 SwiftyJSON 和 Alamofire 在 TableView 上获取 JSON 数据

iphone - 与每个细胞独特合作的一种观点

ios - 如何在Scenekit中的3D模型上添加文字(注释)?

ios - 使用设备倾斜快速移动 SKPhysicsBody

objective-c - coreplot - 总是相同的情节

ios - CIFilter iOS "Program exceeds GLES uniform limits."