保持选定的单元格重载reloadRowsAtIndexPaths UITableView

时间:2012-10-24 08:57:55

标签: objective-c ios uitableview cell

在其中一个视图中有一个UITableView,它经常被更新。 使用“reloadRowsAtIndexPaths”

以经典方式跟踪更改
-(void)refreshCells:(NSArray *)changedCells
{
    NSLog(@"refreshCells %i",[changedCells count]);
    [TableView beginUpdates];
    [TableView reloadRowsAtIndexPaths:changedCells withRowAnimation:UITableViewRowAnimationBottom];
    [TableView endUpdates];
}

问题:如何保留用户上次选择的单元格。每次更新refreshCells后,单元格位置可能会发生变化吗?

1 个答案:

答案 0 :(得分:9)

您可以使用

保存当前选择
NSIndexPath *selectedRow = [self.tableView indexPathForSelectedRow];

重新加载之前,再次使用

选择它
if (selectedRow) {
    [self.tableView selectRowAtIndexPath:selectedRow animated:NO scrollPosition:UITableViewScrollPositionNone];
}

重装后。除非您致电insertRowsAtIndexPaths:deleteRowsAtIndexPaths:,否则单元格位置不会更改。