Reputation: 1543
I am implementing a sectioned tableView that expands section rows when you click on the section header. It works for the most part, but when you select a row, collapse the section, then expand the section, you cannot de select the previously selected row. I'm not sure where I'm going wrong. Another thing is the debugger is printing out [Assert] Unable to determine new global row index for preReloadFirstVisibleRow (0)
which I've never seen before or know if it's related to the issue. I put breakpoints in the didDeselect
and didSelect
functions, and everything hits as expected until the situation I explained above
@objc func headerPressed(sender: UIButton) {
if (!tableDataSource[sender.tag][0].clicked) {
tableDataSource[sender.tag][0].clicked = true
} else {
tableDataSource[sender.tag][0].clicked = false
}
let sections = IndexSet.init(integer: sender.tag)
tableView.reloadSections(sections, with: .fade)
}
func numberOfSections(in tableView: UITableView) -> Int {
return tableDataSource.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableDataSource[section][0].clicked ? tableDataSource[section].count : 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ModifierCell") as? ModifierCell else { return UITableViewCell() }
let mod = tableDataSource[indexPath.section][indexPath.row]
cell.modNameLabel.text = mod.mod.id!
cell.setSelected(mod.selected, animated: true)
cell.checkImage.image = mod.selected ? UIImage(systemName: "checkmark.circle") : UIImage(systemName: "circle")
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.setSelected(tableDataSource[indexPath.section][indexPath.row].selected, animated: false)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! ModifierCell
cell.checkImage.image = UIImage(systemName: "checkmark.circle")
tableDataSource[indexPath.section][indexPath.row].selected = true
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! ModifierCell
cell.checkImage.image = UIImage(systemName: "circle")
tableDataSource[indexPath.section][indexPath.row].selected = false
}
// .....
struct tableElement {
var mod: Modifier
var clicked = false
var selected = false
}
Upvotes: 0
Views: 90
Reputation: 77484
I'm missing some information from your actual setup, but this should fix the issue...
Replace your willDisplay cell
function with this:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if tableDataSource[indexPath.section][indexPath.row].selected {
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
}
//cell.setSelected(tableDataSource[indexPath.section][indexPath.row].selected, animated: false)
}
Upvotes: 1