filterTableViewController.reloadRows 仅在第一次调用时重新加载行
filterTableViewController.reloadRows reloading rows only on first call
我有一个 table 有 3 行,每行带有检查 button.What 我正在做的是当我 select 所有三个按钮我想点击我的取消按钮时不是 table 在同一个控制器上重新加载所有 3 行调用转到自定义单元格 class 其中取消选中设置为 true 并且行是 reloaded.For 第一次尝试它工作正常我可以看到正确的索引再次成为 reloaded.On 当我 select 所有 3 个检查按钮并再次单击取消时,我可以看到要重新加载的正确索引,但调用不会再次转到自定义单元格 class复选框仍然存在 checked.Any 为什么?
我的数组中的索引总是正确的。
取消按钮代码-:
@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)
}
Table代码-:
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
}
自定义 Class-:
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)
}
请记住,单元格是重复使用的,reloadRows 只是告诉行重绘。当用户选中单元格中的复选框时,新的选中状态应保存在底层数据源中,并在 cellForRowAtIndexPath 中标记在单元格中的状态。否则,单元格复选框显示用户最后一次为所有索引设置的状态,而不是基础数据源的状态。
对 "reusable" table 细胞工作原理的根本误解。
假设您的 table 视图足够高,以至于只有 8 个单元格可见。显然需要创建 8 个单元格,滚动时将重复使用它们。
可能不显而易见的是,单元格也在它们被重用时被重用重新加载。换句话说,每次调用 .reloadData
- 即使您只重新加载一个当前可见的单元格 - 该单元格也会被重用。 未重新创建。
因此,关键要点是:任何初始化任务 仅在 首次创建单元格时发生。之后,单元格将被重复使用,如果您想要 "state" 条件 - 例如选中或未选中的按钮 - 由您决定 "reset" 单元格恢复到其原始状态。
正如所写,您的 cellForRowAt
函数仅设置 .filterTableMenu.text
... 它忽略 .isChecked
状态。
您基本上可以通过设置单元格的 .isChecked
值来解决问题,但您也在以比需要更复杂的方式跟踪 on/off 状态。不要使用数组来附加/删除行索引,而是使用布尔数组,并且只使用数组 [行] 来获取/设置值。
那么您的 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
我有一个 table 有 3 行,每行带有检查 button.What 我正在做的是当我 select 所有三个按钮我想点击我的取消按钮时不是 table 在同一个控制器上重新加载所有 3 行调用转到自定义单元格 class 其中取消选中设置为 true 并且行是 reloaded.For 第一次尝试它工作正常我可以看到正确的索引再次成为 reloaded.On 当我 select 所有 3 个检查按钮并再次单击取消时,我可以看到要重新加载的正确索引,但调用不会再次转到自定义单元格 class复选框仍然存在 checked.Any 为什么? 我的数组中的索引总是正确的。
取消按钮代码-:
@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)
}
Table代码-:
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
}
自定义 Class-:
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)
}
请记住,单元格是重复使用的,reloadRows 只是告诉行重绘。当用户选中单元格中的复选框时,新的选中状态应保存在底层数据源中,并在 cellForRowAtIndexPath 中标记在单元格中的状态。否则,单元格复选框显示用户最后一次为所有索引设置的状态,而不是基础数据源的状态。
对 "reusable" table 细胞工作原理的根本误解。
假设您的 table 视图足够高,以至于只有 8 个单元格可见。显然需要创建 8 个单元格,滚动时将重复使用它们。
可能不显而易见的是,单元格也在它们被重用时被重用重新加载。换句话说,每次调用 .reloadData
- 即使您只重新加载一个当前可见的单元格 - 该单元格也会被重用。 未重新创建。
因此,关键要点是:任何初始化任务 仅在 首次创建单元格时发生。之后,单元格将被重复使用,如果您想要 "state" 条件 - 例如选中或未选中的按钮 - 由您决定 "reset" 单元格恢复到其原始状态。
正如所写,您的 cellForRowAt
函数仅设置 .filterTableMenu.text
... 它忽略 .isChecked
状态。
您基本上可以通过设置单元格的 .isChecked
值来解决问题,但您也在以比需要更复杂的方式跟踪 on/off 状态。不要使用数组来附加/删除行索引,而是使用布尔数组,并且只使用数组 [行] 来获取/设置值。
那么您的 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