dev99
dev99

Reputation: 51

Keep the color of text's cell when come back to table

I changed the color of text for the cell clicked in the table. But after the cell is clicked, when i come back to table the text of cell has the original color. Could you give me an advice?

This is the code in "didSelectRowAtIndexPath"

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.highlightedTextColor = [UIColor blueColor];

Thank you

Upvotes: 0

Views: 63

Answers (4)

ncerezo
ncerezo

Reputation: 1371

The reason it's happening is what others are saying: cells are reused. Storing selection state or color will work, however if you just need to make sure that selected cells have a different color for a label than non-selected cells, there's a way that does not require to use a supporting data structure.

You just need to check if the cell being setup at - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is currently selected or not, and that can be achieved with [tableView indexPathForSelectedRow] if your table uses single selection, or [tableView indexPathsForSelectedRows] if it uses multiple selection. The last case requires you to find the current indexPath in the returned array, and might be slower than using the supporting array. But if the selection is simple, then this solution is probably faster, uses less memory and is easier to read (IMO).

Upvotes: 0

fabianfett
fabianfett

Reputation: 729

The problem is that iOS throws away your cell if you scroll away and recreates it when it's needed (you scroll back to the cell).

If I were you, I would subclass UITableViewCell and overwrite

- (void)setSelected:(BOOL)selected animated:(BOOL)animated;  

In there you would have

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
     [super setSelected: selected animated: animated];
     self.textLabel.textColor = selected ? [UIColor blueColor] : [UIColor blackColor];
}

Since iOS UITableView remembers which cell is selected, this should work fine, even when it's recreated.

Upvotes: 0

Greg
Greg

Reputation: 25459

This is happen because the cells are reused, so lets say when you change text colour property of some cell it will be affected as you expect but when you scroll and that cell disappear off the screen it will be put to reuse pool and if it appears again on the screen table view takes some cell from the reuse pool but it's properties will be different so the colour won't persist.

You should keep somewhere, for example in NSMutableArray, info about which table was clicked.

You can add an index path to the array when you click the cell and in cellForRowAtIndexPath: check is this indexPath in the array and if it is change appropriate property.

Upvotes: 0

Caleb
Caleb

Reputation: 124997

after the cell is clicked, when i come back to table the text of cell has the original color. Could you give me an advice?

You need to have the color for each cell stored somewhere other than in the table, so that you can reproduce the colors you want anytime the table redraws itself. Typically, you'll have some sort of data structure that stores the table's data, and that's usually the right place to save any changes the user makes. The table view's data source should have a -tableView:cellForRowAtIndexPath: method that sets the color according to what you've saved, along with any other cell attributes.

Upvotes: 2

Related Questions