PDA

View Full Version : Change background for UITableViewCell




Sergio10
Dec 4, 2009, 11:29 AM
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:

- (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.



North Bronson
Dec 4, 2009, 01:25 PM
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.

Sergio10
Dec 7, 2009, 04:27 AM
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

- (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.

North Bronson
Dec 7, 2009, 05:08 AM
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:"

Sergio10
Dec 7, 2009, 09:33 AM
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?