iosobjective-cuitableviewuitableviewrowaction

set cell.textLabel.textColor causes other section's color inconsisitent in IOS


Im new to IOS development, and now I have issues in cell.textLabel.textColor

what I did is implementing a tableview, and set color according to the section, as I show below, I have 4 section each has 7 rows. enter image description here As you can see I only set text color to red when this label is in 0 section,However, the result is:

enter image description here

the color is wrong for rest of the sections, as you can see ,some of them become red.

but if I uncommented the code in the else scope, set the color to black, it worked.

enter image description here enter image description here


Solution

  • That is the expected behavior.

    UITableView reuses the cells that have the same identifier. So, if you only set them to red in section 0, they will eventually be reused, and since you never told what the reusable state is, they will reuse the red color state.

    You can solve this the way you did (providing a default state when the section is different than zero), or you can implement the method -(void)prepareForReuse in your custom cell (that extends UITableViewCell).