ios - TableView 复选框不接受索引 0 的操作

标签 ios swift uitableview

我有一个表格 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/

相关文章:

Swift 闭包符合类型别名,包括 Any

ios - 使用委托(delegate)和协议(protocol)的 Collection View Cell Action

ios - 如何阻止 UIScrollView 在 View 顶部时暂时向下滚动

ios - Swift 中的模态视图 Controller

ios - 如何调整 UICollectionView 单元格的大小以适合其中的内容?

swift - 我对快速误导性文档的看法是错误的吗?

xcode - 如何在 Swift 中从具有多种内容类型的字典制作表格?

ios - 无法使单元格出队

ios - 嵌入容器中的 UITableView 不刷新

ios - swift 中 spritekit 中的场景转换故障排除