Doug Smith
Doug Smith

Reputation: 29314

Custom UITableViewCell - I set the background image, but how do I set the border color?

I set up a table view in Interface Builder, with Separator set to Single Line, and in the data source class I have this code:

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

And in viewDidLoad I do:

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

But the border color never shows up. I tried setting them in the images, but obviously that presents a lot of centering issues with the images, and having their borders overlap.

UPDATE:

After looking around a bit more, it does indeed get set, but it seems that setting the cell's backgroundView puts what you set as the backgroundView over the separatorColor. When I remove the willDisplayCell: method, the red color shows up fine (same when I select the cell even with the backgroundView set, it will show red until I unselect it). The question is, how can I set the separatorColor if I have a backgroundView set?

Upvotes: 1

Views: 629

Answers (1)

Engin Kurutepe
Engin Kurutepe

Reputation: 6747

One trick you can use is to add a UIView to your background view with a red background color and height 1 point. Another option is to ask your designer to incorporate such a red line in the image file for the background view.

Upvotes: 2

Related Questions