gpt4 book ai didi

ios - 过滤 TableView Controller.reloadRows 仅在第一次调用时重新加载行

转载 作者:行者123 更新时间:2023-11-29 11:46:19 24 4
gpt4 key购买 nike

我有一个包含 3 行的表格,每行带有复选按钮。我正在做的是,当我选择所有三个按钮时,我想单击我的取消按钮,该按钮位于 View 中,而不是同一 Controller 上的表格,以重新加载所有 3 行调用转到自定义单元格类,其中取消选中设置为 true 并重新加载行。第一次尝试它工作正常我可以看到要重新加载的正确索引。第二次当我选择所有 3 个复选按钮并再次单击取消我可以看到要重新加载的正确索引,但调用不会再次自定义单元格类复选框仍保持选中状态。知道为什么吗?我的数组中的索引总是正确的。

取消按钮代码-:

@IBAction func cancelDataItemSelected(_ sender: UIButton) {
for index in selectedButtonIndex{
let indexPath = IndexPath(item: index, section: 0)
print(selectedButtonIndex)
filterTableViewController.reloadRows(at: [indexPath], with: UITableViewRowAnimation.none)
}
selectedButtonIndex .removeAll()
print(selectedButtonIndex)
}

表格代码-:

extension filterControllerViewController:UITableViewDataSource,UITableViewDelegate
{
// NUMBER OF ROWS IN SECTION
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return ControllerData.count
}

// CELL FOR ROW IN INDEX PATH
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let Cell = tableView.dequeueReusableCell(withIdentifier: "filterCell", for: indexPath) as! ControllerCellTableViewCell
Cell.filterTableMenu.text = ControllerData[indexPath.item]
Cell.radioButtonTapAction = {
(cell,checked) in
if let radioButtonTappedIndex = tableView.indexPath(for: cell)?.row{
if checked == true {
self.selectedButtonIndex.append(radioButtonTappedIndex)
}
else{
while self.selectedButtonIndex.contains(radioButtonTappedIndex) {
if let itemToRemoveIndex = self.selectedButtonIndex.index(of: radioButtonTappedIndex) {
self.selectedButtonIndex.remove(at: itemToRemoveIndex)
}
}
}
}
}
return filterCell
}

自定义类-:

var radioButtonTapAction : ((UITableViewCell,Bool)->Void)?
//MARK-:awakeFromNib()
override func awakeFromNib() {
super.awakeFromNib()
filterTableSelectionStyle()
self.isChecked = false
}

// CHECKED RADIO BUTTON IMAGE
let checkedImage = (UIImage(named: "CheckButton")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal))! as UIImage
// UNCHECKED RADIO BUTTON IMAGE
let uncheckedImage = (UIImage(named: "CheckButton__Deselect")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal))! as UIImage
// Bool STORED property
var isChecked: Bool = false {
didSet{
// IF TRUE SET TO CHECKED IMAGE ELSE UNCHECKED IMAGE
if isChecked == true {
TableRadioButton.setImage(checkedImage, for: UIControlState.normal)
} else {
TableRadioButton.setImage(uncheckedImage, for: UIControlState.normal)
}
}
}
// FILTER CONTROLLER RADIO BUTTON ACTION

@IBAction func RadioButtonTapped(_ sender: Any) {
isChecked = !isChecked
radioButtonTapAction?(self,isChecked)
}

最佳答案

对“可重用”表格单元格工作原理的根本误解。

假设您的表格 View 足够高,以至于只有 8 个单元格可见。显然需要创建 8 个单元格,并且在您滚动时将重复使用它们。

可能明显的是,单元格在重新加载时被重用。换句话说,每次 .reloadData 被调用时——即使您只重新加载一个当前可见的单元格——该单元格也会被重用。它不是重新创建的。

因此,关键要点是:任何初始化任务在首次创建单元格时发生。之后,单元格将被重复使用,如果您想要“状态”条件(例如已选中或未选中的按钮),则由您将单元格“重置”为其原始状态。

如所写,您的 cellForRowAt 函数仅设置 .filterTableMenu.text ... 它忽略 .isChecked 状态。

您基本上可以通过设置单元格的 .isChecked 值来解决问题,但是您也会以比需要的复杂得多的方式跟踪开/关状态。不要使用数组来附加/删除行索引,而是使用 bool 数组,并且只使用数组 [行] 来获取/设置值。

然后你的 cellForRowAt 函数看起来像这样:

// CELL FOR ROW IN INDEX PATH
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let filterCell = tableView.dequeueReusableCell(withIdentifier: "filterCell", for: indexPath) as! ControllerCellTableViewCell

// set the label in filterCell
filterCell.filterTableMenu.text = ControllerData[indexPath.item]

// set current state of checkbox, using Bool value from out "Tracking Array"
filterCell.isChecked = self.selectedButtonIndex[indexPath.row]

// set a "Callback Closure" in filterCell
filterCell.radioButtonTapAction = {
(checked) in
// set the slot in our "Tracking Array" to the new state of the checkbox button in filterCell
self.selectedButtonIndex[indexPath.row] = checked
}

return filterCell

}

您可以在此处查看工作示例:https://github.com/DonMag/CheckBoxCells

关于ios - 过滤 TableView Controller.reloadRows 仅在第一次调用时重新加载行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43686354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com