One might be tempted to also set the cell’s background in here but you will get confusing results if you do. The iPhoneOS does some tricky stuff creation of the cell and actually displaying it to maximize performance but still allow for a blue selection behind the cell’s content without you having to think about how these cells are composited.

From a WWDC video I learned that the best place to put such alternating is not in the place you would normally use to set up a cell. There is a method that gets called right before the drawing and this gives you the opportunity to do some “last millisecond changes” before the cell actually draws itself in it’s place.

Note the C-ish way to make the color alternate. % is the modulo operator. And ?: is a shorthand for an if. condition?true-value:false-value. This is called when the cell “will be displayed” as you can see from the name.

Now the row numbers start from zero for every section. So we have to find a way to get the real row number for the whole table view. The only way I know of is to actually do a bit of counting ourselves. But since there are already delegate methods to return the number of sections and the number of rows for any given section this is fairly straightforward.