Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Sergio10

macrumors regular
Original poster
Oct 3, 2007
137
0
Hi,

I need to change background for the item(UITableViewCell).
And problem is next: when change background with image once it works Ok, but when I change for the second time(after refreshing table) it doesn't change.
It works only when use solid colors for changing(blueColor, greenColor) background.

So here is a code:
PHP:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
 static NSString *CellIdentifier = @"Cell";

 MyCell *cell = [[[MyCell alloc] initWithFrame:CGRectZero  reuseIdentifier:CellIdentifier] autorelease];

 if(...)
{
 [cell setMyBackground:@"someBakground.png"];
}
else
{
 [cell setMyBackground:@"someBakground2.png"];
}

return cell;
}

. . .

- (void)setMyBackground:(NSString*)anImage
{
[self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:anImage]]];
}
How to solve the problem?

Thank you.
 
What version of iPhone OS are you building for? Different versions of the OS seemed to handle the cell's background color in different ways. Are you building for 2.X because you call "initWithFrame:reuseIdentifier:"?

Does your "setMyBackground:" method belong to your "MyCell" class?

You are calling "setMyBackground:" in "tableView:cellForRowAtIndexPath:". This might be a better method to call in your "tableView:willDisplayCell:forRowAtIndexPath:" method.

You are also creating a new cell each time you call "tableView:cellForRowAtIndexPath:" You want to check to see if your table view has a reusable cell available before you alloc and init an brand new instance.
 
What version of iPhone OS are you building for? Different versions of the OS seemed to handle the cell's background color in different ways. Are you building for 2.X because you call "initWithFrame:reuseIdentifier:"?
I use iPhone SDK 3.0. How to create cell for 3.0 iPhone OS?
Does your "setMyBackground:" method belong to your "MyCell" class?
Yes. Here is my implementation of this method
PHP:
- (void)setMyBackground:(NSString*)aBackground
{
  [self setBackgrounColor:[UIColor colorWithPatternImage:[UIImage imageNamed:aBackground]]];
}

You are calling "setMyBackground:" in "tableView:cellForRowAtIndexPath:". This might be a better method to call in your "tableView:willDisplayCell:forRowAtIndexPath:" method.
Sorry, but I can't change logic in such way.

You are also creating a new cell each time you call "tableView:cellForRowAtIndexPath:" You want to check to see if your table view has a reusable cell available before you alloc and init an brand new instance.
I think reuse cell because use "initWithFrame: reuseIdentifier". Could you please describe how to fix it if I'm wrong in more details?

Thank you.
 
I think reuse cell because use "initWithFrame: reuseIdentifier". Could you please describe how to fix it if I'm wrong in more details?

When you alloc and init a new table view cell, you give it a reuse identifier, but you get the benefit by checking to see if the table view can give you a cell *before* you go to the trouble of making a new cell.

You pick a reuse identifier, then you send that identifier to the table view to ask for a cell. If that cell does not exist (it returns nil), then you go to all the trouble of making a new cell.

Sorry, but I can't change logic in such way.

I have to ask why you can not use that method? Apple recommends that changes to the graphics state of a cell (such as background color) should be made in "tableView:willDisplayCell:forRowAtIndexPath:". In fact, I can remember times when I was trying to set background colors and I was having problems when I changed the colors in "tableView:cellForRowAtIndexPath:"
 
tableView:willDisplayCell:forRowAtIndexPath:
It helped. Thank you.

When you alloc and init a new table view cell, you give it a reuse identifier, but you get the benefit by checking to see if the table view can give you a cell *before* you go to the trouble of making a new cell.

You pick a reuse identifier, then you send that identifier to the table view to ask for a cell. If that cell does not exist (it returns nil), then you go to all the trouble of making a new cell.

Could you please show me an example in my case?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.