I’m trying to toggle the color of UITableCell when it is clicked.
Code is below
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
RowCell *cell = (RowCell*) [tableView cellForRowAtIndexPath:indexPath];
[cell toggle];
}
The toggle actually works, except that it seems like it hilight more than 1 cell.
i.e if 10 rows covers the currently visible area, clicking on the first row will hilight row 1, row 11, row 21 etc instead of just row 1 that I clicked.
How can I prevent that from happening?
Thanks,
Tee
Make sure that you are properly configuring your cells in
tableView:cellForRowAtIndexPath:. Remember, by usingdequeueReusableCellWithIdentifier:you are reusing table view cells. The cell at row 11 is probably the same cell that was previously shown at row 1.Set the highlight state of each cell before it is returned by
tableView:cellForRowAtIndexPath:. Note that this will probably require you to store the highlight state of each row somewhere other than in the cells themselves.