Setting background image of custom table view cells

15,267

Solution 1

Try setting the backgroundView to an imageView of the image.

Code example from Jason Moore (with TomH's correction):

cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]] autorelease];

Solution 2

cell.backgroundImage no longer exists. Instead use:

cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]];

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html

Solution 3

I've been doing this using the following UITableViewDelegate method, and setting the cell.backgroundColor property. It gets called last, right before the cell is actually drawn on the screen:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row%2)
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TableCell-BG-Dark.png"]];
else 
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TableCell-BG-Light.png"]];

}

And yes, I'm using a custom subclass of UITableViewCell.

Solution 4

try doing this :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DateCellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:DateCellIdentifier] autorelease]
            UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"gradient.png"]];
            [cell setBackgroundView:img];
            [img release];
        } 
        cell.textLabel.text = @"Text";
}
Share:
15,267
msk
Author by

msk

Updated on June 04, 2022

Comments

  • msk
    msk almost 2 years

    I've tried numerous ways of setting the background image of an unselected table cell, but without success:

    1- In IB setting the image field
    2- cell.contentView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"list.png"]];
    3- cell.imageView.image = [UIImage imageNamed:@"list_selected.png"];
    

    All seem to fail. The image setting for selected cell works, but not for an unselected cell. Anyone having any idea what might be wrong here?

    Thanks

  • msk
    msk about 14 years
    Thanks for the reply, Gaurav! I am using a custom built cell for the table view. I tried your above code as is, and putting the background image setting code in the constructor of the custom cell controller class, but still in vain.
  • Yarek T
    Yarek T about 14 years
    note that this seems to work reliably only with a custom cell, not the stock one. In the stock cell the background still applies but all the labels have their background set to white :\
  • Jason Moore
    Jason Moore about 14 years
    cell.backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]];
  • TomH
    TomH almost 13 years
    I believe the above will leak -- add an autorelease on the end
  • abbood
    abbood over 11 years
    shouldn't this be cell.backgroundView = [[UIImageView alloc] initWithImage:.... a UITableViewCell doesn't have a backgroundImage property