I’m a newbie to iPhone development. I have a table view with multiple sections in it. I’m changing the color of cells like this and it works fine.
if ([indexpath row]%2)
{
trackCell.contentView.backgroundColor = [UIColor clearColor];
}
else
{
trackCell.contentView.backgroundColor = [UIColor colorWithRed:212/255.0 green:212/255.0 blue:212/255.0 alpha:0.1];
}
But now the last cell from one section has the same color as the first cell in the next section. How can I solve this?
the problem is that if cell is in section 1 (or 2, 3, …)
and the previous section has an odd number of cells, it ends with the same color of the last cell of previous cell.
so, you need to check the previous section number of cells, and if that number is odd, invert your “if” statement, and leave it as it is now if it is even
EDIT:
EDIT 2:
seeing “Will Jenkins”… he’s right… you need to see all previous sections, not just the previous one…
his way to count all cells in a section is not so good and fast as mine, but his answer is the right one, +1 for him… anyway, the best way could be to combine his loop and my call to tableView:tableView numberOfRowsInSection: