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

时间:2013-07-06 18:55:40

标签: ios objective-c uitableview

我在Interface Builder中设置了一个表视图,其中Separator设置为Single Line,而在数据源类中我有这个代码:

- (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];
}

但边框颜色从未出现过。我尝试在图像中设置它们,但显然这会给图像带来许多中心问题,并且边框重叠。

UPDATE:

看了一下之后,确实设置好了,但似乎设置单元格的backgroundView会将你设置为backgroundView 的内容放在 separatorColor上。当我删除willDisplayCell:方法时,红色显示正常(当我选择单元格时,即使设置了backgroundView,它也会显示红色,直到我取消选择它)。问题是,如果我设置了backgroundView,如何设置separatorColor?

1 个答案:

答案 0 :(得分:2)

您可以使用的一个技巧是在背景视图中添加UIView,背景颜色为红色,高度为1磅。另一个选择是让您的设计师在图像文件中加入这样的红线作为背景视图。

相关问题