mtet88 mtet88 - 1 year ago 146
iOS Question

Maintain offset when reloadRowsAtIndexPaths

I'm trying to reload a single tableViewCell but it scrolls to the top every time I do it... I'm not adding nor deleting cells, I just want to change the color of the selected cells.

This is what I do in the cellForRowAtIndexPath:

SMPChoiceViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChoiceCell" forIndexPath:indexPath];
SMPChoice *choice = self.choices[indexPath.row - 1];
cell.choiceTextLabel.text = choice.text;

if ([self.selectedChoices indexOfObject:choice] != NSNotFound) {
cell.choiceTextLabel.textColor = [UIColor purpleColor];
} else {
cell.choiceTextLabel.textColor = [UIColor blackColor];

And this is what I do in the didSelectRowAtIndexPath

if ([self.selectedChoices indexOfObject:choice] != NSNotFound) {
[self.selectedChoices removeObject:choice];
} else {
[self.selectedChoices addObject:choice];

CGPoint offSet = [tableView contentOffset];

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView setContentOffset:offSet animated:NO];

But it just jumps, any suggestion?

I followed this thread but it didn't solved my question Calling reloadRowsAtIndexPaths removes tableView contentOffset

Answer Source

I know this is an old question, but I had this same issue and couldn't find the answer anywhere.

After reloadRowsAtIndexPaths:withRowAnimation:, the tableView determines its offset using the estimated heights given in tableView:estimatedHeightForRowAtIndexPath:. So unless the value you return there is accurate, implementing it will cause your tableView's offset to change after the reload. I unimplemented tableView:estimatedHeightForRowAtIndexPath: and the problem was fixed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download