save selected row in UITableView after reloadData

后端 未结 14 933
后悔当初
后悔当初 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:09

    A lot of these answers are basically using hacks to re-select the row that was previously highlighted by finding out what the previously selected row was from the table view which seems like a bad way to go about it, because usually when your tableview reloads, there is a change in the data that is populating the tableview.

    So rather base you re-selection of the row based on your data

    let data = ["some", "array", "of", "data"]
    let selected = "of" // this should be populated from didSelectRowAt indexPath:
    
    tableView.reloadData()
    
    if let index = data.firstIndex(of: selected) {
      tableView.selectRow(at: IndexPath(row: index, section: 0), animated: false, scrollPosition: .none) // the "of" row should now be highlighted, regardless if the data changed order
    }
    

提交回复
热议问题