Alfa
Alfa

Reputation: 619

In Objective C UITableVIew how to retain scroll position after calling reload

In Objective C UITableVIew how to retain scroll position after calling reload

While selecting a cell I call reload function of the UITableView but after reload I want the scroll to maintain its position (i.e, Position where the cell was tapped)

Upvotes: 4

Views: 1175

Answers (3)

USER_NAME
USER_NAME

Reputation: 1037

Two lines and you get the desired result:

 NSArray *indexPaths = tableView.indexPathsForVisibleRows;
[tableView reloadRowsAtIndexPaths:indexPathswithRowAnimation:UITableViewRowAnimationNone];'

Upvotes: 0

Abubakr Dar
Abubakr Dar

Reputation: 4078

You could do something like this.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

Also if you want to scroll to some particular position, you can use this.

  [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];

Upvotes: 1

Lucas Huang
Lucas Huang

Reputation: 4016

You can use reloadRowsAtIndexPaths:withRowAnimation: instead of reloadData.

Upvotes: 1

Related Questions