简体   繁体   English

重新加载部分后无法取消选择 tableView 行

[英]Cannot deselect tableView row after reloading section

I am implementing a sectioned tableView that expands section rows when you click on the section header.我正在实现一个分段的 tableView,当您单击 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.它在大多数情况下都有效,但是当您 select 一行时,折叠该部分,然后展开该部分,您无法取消 select 先前选择的行。 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.另一件事是调试器正在打印出[Assert] Unable to determine new global row index for preReloadFirstVisibleRow (0) ,我以前从未见过或知道它是否与问题有关。 I put breakpoints in the didDeselect and didSelect functions, and everything hits as expected until the situation I explained above我在didDeselectdidSelect函数中设置了断点,一切都按预期进行,直到我在上面解释的情况

@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
}

I'm missing some information from your actual setup, but this should fix the issue...我从您的实际设置中遗漏了一些信息,但这应该可以解决问题......

Replace your willDisplay cell function with this:将您的willDisplay cell function 替换为:

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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM