2

I'm getting really desperate trying to add a UIImageView to UITableViewCell.backgroundView. All my efforts have resulted in this crappy rendering:

alt text http://img.skitch.com/20091123-ch8wk6pdxqkrn9tpftnhusigcy.jpg

It looks like the cell's label's white background is sitting on top of cell's background and covering portions of it.

I tried setting the label's background color to clear, or some other color and it does not have any event. It is always white.

The reason I know it's the text label's background causing this white area is that if I don't do [cell setText:@"Cell text here"]; the white area is gone and I see just the cell's background image.

Here's the code that I'm using. The table view is added in the .xib file and UITableView is added to UIViewController:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [myCollection.items count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger rowIndex = indexPath.row;
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"darkCellBackground.png"]];
        cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"darkCellBackground.png"]];
    }


    [cell setText:@"Cell text here"];


    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    // AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
    // [self.navigationController pushViewController:anotherViewController];
    // [anotherViewController release];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

I'm sure I'm doing something wrong but cant quite figure out what.

subjective-c
  • 1,833
  • 9
  • 25
  • 35

6 Answers6

7

The solution is here: Changing UITableViewCell textLabel background color to clear

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
     [[cell textLabel] setBackgroundColor:[UIColor clearColor]];
     [[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];
}
Community
  • 1
  • 1
Simon Woodside
  • 7,175
  • 5
  • 50
  • 66
3

What you want is:

cell.backgroundColor = [UIColor colorWithPatternImage:[[UIImage imageNamed:@"some.png"]stretchableImageWithLeftCapWidth:320 topCapHeight:44]];
ADude
  • 345
  • 5
  • 14
2

Did you try setting the cell's textLabel to not be opaque?

cell.textLabel.opaque = NO;

Or, if your background is a solid color where the label is, you could set the label's background color to the appropriate color.


Deprecation sidenote:

[UITableViewCell setText:] is deprecated in OS 3.0 - you shouldn't use it (if you're building for 3.0+) as it could disappear in the future. Instead, you should use UITableViewCell's textLabel property to set the text of the UILabel directly.


Memory management sidenote:

You're leaking two instances of UIImageView for every UITableViewCell you create using this code. These lines

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

should be changed to this

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

Or you could use release instead of autorelease (doing it properly; don't try to just substitute release for autorelease, of course). Either way, the UITableViewCell takes ownership of the background views and because you alloc'd the objects you need to release your ownership of the objects.

Bryan Henry
  • 8,348
  • 3
  • 34
  • 30
  • Thanks. I'll try setting the opaque to no like you suggested. Thanks for the pointer to the memory leak. As I was pasting that code in the StackOverflow question box, I looked at that and wondered what will happen to those instances. Still trying to learn to think about memory management. As for your depreciation note, I'm creating a build that will work on 2.2 and 3.0, so I'm not sure textLabel will exists on 2.2 devices, that's why I'm still sticking with it. – subjective-c Nov 23 '09 at 21:21
  • This doesn't work for me. I also tried setting `cell.textLabel.backgroundColor = [UIColor clearColor];` which didn't help either. Perhaps something else needs to be set opaque. – Simon Woodside Nov 29 '10 at 05:54
1

Maybe you should take a look at this:

How can I set the background color of a cell in UITableView on iphone?

Community
  • 1
  • 1
AndersK
  • 35,813
  • 6
  • 60
  • 86
  • It doesn't appear as if he's trying to set the backgroundColor of the UITableViewCell - he's using a custom image in a UIImageView for both his backgroundView and selectedBackgroundView. Problems like this are typically related to the fact that many UITableViewCell elements are opaque by default for performance reasons. Obviously its ideal to have the opaque flag on when possible, but if the background of the cell is not a solid color, its usually not possible. – Bryan Henry Nov 23 '09 at 06:32
0
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

    cell.backgroundView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list-view-bg.png"] highlightedImage:[UIImage imageNamed:@"list-view-bg.png"]];


}
goku
  • 169
  • 1
  • 13
0

Set the table view background to clear color

Lauren
  • 1