セクションヘッダーをクリックするとセクション行を展開するセクション化されたtableViewを実装しています。ほとんどの場合は機能しますが、行を選択してセクションを折りたたんでからセクションを展開すると、以前に選択した行の選択を解除することはできません。どこが間違っているのかわかりません。もう1つは、デバッガーが[Assert] Unable to determine new global row index for preReloadFirstVisibleRow (0)
これまでに見たことのない、または問題に関連しているかどうかを知っている印刷を行うことです。didDeselect
anddidSelect
関数にブレークポイントを設定すると、上記で説明した状況になるまで、すべてが期待どおりにヒットします。
@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
}
実際の設定からいくつかの情報が欠落していますが、これで問題が解決するはずです...
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)
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加