【问题标题】:Cannot deselect tableView row after reloading section重新加载部分后无法取消选择 tableView 行
【发布时间】:2020-11-21 15:12:10
【问题描述】:

我正在实现一个分段的 tableView,当您单击节标题时会展开节行。它在大多数情况下都有效,但是当您选择一行,折叠该部分,然后展开该部分时,您无法取消选择先前选择的行。我不确定我哪里出错了。另一件事是调试器正在打印出我以前从未见过的[Assert] Unable to determine new global row index for preReloadFirstVisibleRow (0),或者知道它是否与问题有关。我在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
}

【问题讨论】:

  • 您不能取消选择,但是调用了 didDeselectRowAt 委托?您是否尝试重新加载完整的表格取消选择,至少看看它是否有效。按下时 sender.tag 的内容是什么?请注意,您可以将功能主义者简化如下:@objc func headerPressed(sender: UIButton){ tableDataSource[sender.tag][0].clicked.toggle() letsections = IndexSet.init(integer: sender.tag) tableView。 reloadSections(sections, with: .fade) }
  • didDeselectRowAt 委托仅在您选择时调用,然后取消选择,但如果您选择,折叠行,然后展开,您选择的单元格显示为选中,但无法取消选择,并且在尝试取消选择时一行,没有调用didDeselectRowAt函数
  • sender.tag 是被点击展开的部分
  • @NoahIarrobino - 您是允许多选还是单选?如果在要折叠的部分中选择了一行,是否希望在展开该部分时保持选中状态?
  • 我允许多选,是的,我希望选定的单元格在折叠时保持选中状态,并且确实如此。问题是,如果该单元格已被选中、折叠然后展开,则您无法取消选择该单元格。即使展开后,您也应该能够取消选择该单元格

标签: ios swift uitableview tableview


【解决方案1】:

我在您的实际设置中遗漏了一些信息,但这应该解决问题...

用这个替换你的 willDisplay cell 函数:

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

【讨论】:

    猜你喜欢
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多