views:

55

answers:

2

Dear all,

When I set repeating background image to a UITableView by using the following code is ok:

tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];

but when I use it to set repeating-background image to a UITableViewCell by following code, nothing happen, I don't know why, I also cannot set background color to this cell too.

cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];

Is there anyone know the way to so this? please help me!

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

which would go inside this code block:

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

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

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-on.png"]];
    }    
// Configure cell

return cell;
}

You can also see how to set the selected image in inside there too: selectedBackgroundView.

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

I'm unsure about repeating, but I'm pretty sure backgroundView can have contentMode set on it.

Kolin Krewinkel
Dear Kolin, thanks for your answer! about this solution, I knew it already. I ask for repeating-background image in order to not have to change background image in landscape mode.
Son Nguyen
[[UIImage imageNamed:@"bg.png"] drawAsPatternInRect:yourRect];
Kolin Krewinkel
See if this works if you make a Rect inside your contentview.
Kolin Krewinkel
Thanks Kolin, the problem is resolved
Son Nguyen
A: 

Oh, it is more easier than I think. I just need to set background image by following code:

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

and the whole cell background view is automatically filled with this background image. :)

Son Nguyen