0

I have a UITableView.
I can select multiple cells make them highlighted.
But, whenever I reload App, the selected and highlighted state disappears.
I think I need to put some codes in 'viewDidAppear' and 'didSelectRowAt'. But I don't have any idea of specific codes.
What do I have to put in this syntax?

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        tableView.reloadData()
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


    }

}
Hoo
  • 135
  • 13
  • You need to maintain the information regarding selection of a cell and check that in `cellForRowAt` and set the selected property accordingly. – Rakesha Shastri Oct 23 '18 at 07:41

1 Answers1

0

I recommend u add to dataSource item field selected, and in method cellForRowAtindexPath configure by your dataSource items.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cellClass = (self.cellClass(for: indexPath) ?? self.cellClasses.first) else { return UITableViewCell() }

        let cell = tableView.dequeueReusableCell(withIdentifier: cellClass.identifier, for: indexPath)

        if let configurable = cell as? ConfigurableComponent, let object = self.object(at: indexPath) {
            let item = self.dataSource[indexPath.row]

            configurable.selected =  item.selected
        }

        return cell
    }

When call didSelectRowAt, just take item and change selected field, sample:

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      let item = self.dataSource[indexPath.row]
      item.selected = !item.selected
      tableView.reloadData()

  }
Vitaliy Rusinov
  • 256
  • 1
  • 10
  • Thank you for your reply, but unfortunately, I cannot understand of your answer. First, I'm beginner of Swift. And I thought that, to reload selected row, I need to save selected row data in advance. Does your code have this process? I don't know what the codes in your answer can do this. 'Reload' in my question means 'load again after killing my app'. If there's something to know in advance, please let me know. Thank you again. – Hoo Oct 23 '18 at 08:20
  • To save state after kill your app, you should use UserDefaults or CoreData. – Vitaliy Rusinov Oct 23 '18 at 08:29
  • Just save your dataSource to UserDefaults in didSelectRowAt. Read it here: https://stackoverflow.com/questions/29986957/save-custom-objects-into-nsuserdefaults – Vitaliy Rusinov Oct 23 '18 at 08:31
  • Thank you for comment, but what is dataSource you mentioned exactly? A new property declared in class ViewController? – Hoo Oct 23 '18 at 08:39
  • Yes it's for example array : var dataSource = [DisplayItem]() where DisplayItem is struct DisplayItem { var selected: Bool; let cellName: String; let cellImageUrlString: String ... } – Vitaliy Rusinov Oct 23 '18 at 09:43
  • And you should obtain this array in viewDidAppear from UserDefaults or CoreData than call self.tableView.reloadData(). – Vitaliy Rusinov Oct 23 '18 at 09:47
  • You mean, i need to call NSIndexPath to reload selected row? But, as you know, NSUserDefault cannot save NSIndexPath. – Hoo Oct 23 '18 at 09:53
  • Please read this manual https://www.raywenderlich.com/7569-getting-started-with-core-data-tutorial. – Vitaliy Rusinov Oct 23 '18 at 13:10