我有一个表格 View ,其中包含带有复选框的项目。用户可以选择任何项目(也可以多项选择),然后可以按“继续”按钮进行付款。
我使用按钮作为复选框。因此,如果用户按下按钮,或者选择表格行,这两种情况都会处理该功能。
现在,我的继续按钮开始处于禁用状态。每当用户使用 didSelectRowAt
方法或复选框按钮操作按下任何项目时,才会启用“继续”按钮。
但是现在,每当我按下第一个项目或第一个复选框按钮时,我的继续按钮都不会启用。如果我按第二个项目或第二个复选框,它就会起作用。我不知道为什么。
这是我的 didSelectRowAt
代码和我的复选框按钮操作代码:
#更新:
var selectedIndexPathArray = Array<NSIndexPath>()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "itemcell", for: indexPath) as! itemTableViewCell
cell.checkboxBtn.isUserInteractionEnabled = false
if selectedIndexPathArray.contains(indexPath as NSIndexPath) {
cell.checkboxBtn.setImage(UIImage(named: "enabled"), for: .normal)
proceedbutton.isUserInteractionEnabled = true
} else {
cell.checkboxBtn.setImage(UIImage(named: "disabled"), for: .normal)
proceedbutton.isUserInteractionEnabled = false
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if selectedIndexPathArray.contains(indexPath as NSIndexPath) {
let index = selectedIndexPathArray.index(of: indexPath as NSIndexPath)
selectedIndexPathArray.remove(at: index!)
} else {
selectedIndexPathArray.append(indexPath as NSIndexPath)
}
tableView.reloadData()
}
提前致谢!
最佳答案
是的,因为第一次单击按钮时,proceedbutton
将被调用,此时 selectedIndexPathArray
将没有任何值,意味着 nil。
所以第一次你的else部分将被执行。
.
.
.
else {
selectedIndexPathArray.append(indexPath! as NSIndexPath)
cell?.checkboxBtn.setImage(UIImage(named: "disabled"), for: .normal)
proceedbutton.isUserInteractionEnabled = false
}
当您按 agin 时,您的 if 条件将为 true,因此它将启用
关于ios - TableView 复选框不接受索引 0 的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46641892/