表视图checkbox不接受索引0的操作

我有一个表格视图,其中有checkbox的项目。 用户可以select任何项目(多选也),他们可以按继续button付费。

我用一个button作为checkbox。 所以如果用户按下button,或者他们select了一个表格行,这两种情况都是处理该function的。

所以现在,我的继续buton开始禁用。 每当用户使用didSelectRowAt方法或checkboxbutton操作按下任何项目时,只有继续button将被启用。

但现在,无论何时我按下第一个项目或第一个checkboxbutton,我的继续button都没有启用。 如果我按第二个项目或第二个checkbox,它的作品。 我不知道为什么。

这里是我的代码didSelectRowAt和我的checkboxbutton操作代码:

#更新:

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

提前致谢!

是的,因为第一次如果你点击buttonproceedbutton将被调用,并在那时selectedIndexPathArray将没有任何值意味着零。

所以第一次你的其他部分将被执行。

 . . . else { selectedIndexPathArray.append(indexPath! as NSIndexPath) cell?.checkboxBtn.setImage(UIImage(named: "disabled"), for: .normal) proceedbutton.isUserInteractionEnabled = false } 

而当你按agin,那么你的条件将是真实的,所以它将被启用

在你的proceed()函数上试试这个if-else条件。

  if selectedIndexPathArray.contains(indexPath! as NSIndexPath) { electedIndexPathArray.remove(at: index!) cell?.checkboxBtn.setImage(UIImage(named: "disabled"), for: .normal) proceedbutton.isUserInteractionEnabled = false } else { selectedIndexPathArray.append(at: index!) cell?.checkboxBtn.setImage(UIImage(named: "enabled"), for: .normal) proceedbutton.isUserInteractionEnabled = true } 

删除单元格中button的select器,默认设置userInteraction forbutton为false。无需每次都设置。 执行didSelectRow上的所有内容。

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: itemscell, for: indexPath) as! itemsTableViewCell cell?.checkboxBtn.isUserInteractionEnabled = false if selectedIndexArray.contains(indexPath.row) { 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 selectedIndexArray.contains(indexPath.row) { let index = selectedIndexPathArray.index(of: indexPath.row) selectedIndexArray.remove(at: index!) } else { selectedIndexArray.append(indexPath.row) } tableView.reloadData() }