save selected row in UITableView after reloadData

后端 未结 14 1030
后悔当初
后悔当初 2020-12-24 11:20

I write custom jabber client in iphone.

I use xmppframework as engine.

And I have UITableViewController with NSMutableArray for repesent contact list.

<
14条回答
  •  情深已故
    2020-12-24 12:04

    Adding a bit to Marco's answer:

    First, if you're using multiple selection, you can add this method to your ViewController and call it whenever you need to call [tableView reloadData]:

    - (void)reloadTableView
    {
        NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];
        [self.tableView reloadData];
        for (NSIndexPath *path in indexPaths) {
            [self.tableView selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
    }
    

    Second, if you're in a UITableViewController and you want to preserve selection after tableView appears there's a feature in UITableViewController: clearsSelectionOnViewWillAppear you can turn on or off.

    See here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewController_Class/Reference/Reference.html

提交回复
热议问题