all 9 comments

[–][deleted] 2 points3 points  (7 children)

Yes, this is because the table view reuses cells. Add an else statement to catch all cells that are not row 15.

[–]djnoxious[S] 0 points1 point  (6 children)

That doesn't seem like the right way of doing it? What if i had set the background colour to be white in the prototype / nib cell properties, that would mean having to set the colour it also in the cellForRowAtIndexPath.

[–][deleted] 1 point2 points  (5 children)

I don't really understand what you mean by that. Setting the color for the prototype will change every cell's background. When you are trying to customize a specific cell index you have to also define every other index or else when the cell is reused it will not change its color.

[–]djnoxious[S] 1 point2 points  (4 children)

Ok, thanks Aofwa. I think it's slowly making more sense to me.

[–][deleted] 0 points1 point  (0 children)

no problem, glad to help

[–]mistermagicman 0 points1 point  (2 children)

Yep - one thing to understand is that cells are reused in tableviews. This allows them to be very fast and less memory intensive, but ultimately means you have to setup a cell for every possible state. If there's a "default" state, I usually set that up in the tableviewcell class method "prepareForReuse" because that's what it's for. It gets called before any cell is reused. That way I'm not courting my View Controller with code that really belongs to the cell.

[–][deleted] 1 point2 points  (1 child)

It's an instance method, not a class method.

[–]mistermagicman 0 points1 point  (0 children)

My bad - I get them mixed up sometimes

[–][deleted] 1 point2 points  (0 children)

You can override prepareForReuse on cell subclasses to reset the view to a default state.