表视图复选框不接受索引 0 的操作 [英] Table view check box not accepting action for index 0

查看:21
本文介绍了表视图复选框不接受索引 0 的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格视图,其中包含带有复选框的项目.用户可以选择任何项目(也可以进行多选),然后按继续"按钮进行支付.

I have a table view, in which I have the items with check boxes. Users can select any items (multiselection also) and they can press the Proceed button to pay.

我使用按钮作为复选框.所以如果用户按下按钮,或者他们选择了一个表格行,这两种情况都会被处理.

I was using a button as a check box. So if the user presses the button, or they select a table row, both cases are handled the function.

所以现在,我的继续按钮开始被禁用.每当用户使用 didSelectRowAt 方法或复选框按钮操作按下任何项目时,才会启用 Proceed 按钮.

So now, my proceed buton starts as disabled. Whenever the user presses any items using the didSelectRowAt method or check box button action, only then the Proceed button will be enabled.

但是现在,每当我按下第一个项目或第一个复选框按钮时,我的继续按钮都不会启用.如果我按下第二个项目或第二个复选框,它会起作用.我不知道为什么.

But now, whenever I press the first item or the first check box button, my proceed button is not getting enabled. If I press the second item or the second check box, it works. I don't know why.

这是我的 didSelectRowAt 代码和我的复选框按钮操作代码:

Here is my code of didSelectRowAt and my check box button action code:

#更新:

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

提前致谢!

推荐答案

去掉单元格中按钮的选择器,按钮默认设置 userInteraction 为false,不需要每次都设置.在 didSelectRow 上执行所有操作.

Remove selector for button in cell and default set userInteraction for button is false.No need to set it everytime. Perform everything on 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()
    }

这篇关于表视图复选框不接受索引 0 的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆