lundi 9 octobre 2017

Table view check box not accepting action for index 0

I have a table view , in which i have the items with check box. User can select any items ( multiselection also ) and they can press proceed button to pay.

In that now, i was using button for check box. So if user select button or did select method both will handle the function,

So, now my proceed buton will be disabled. When ever user press any utem using did select method or check box button action then only proceed button will be enable.

But now, when ever i press first item or first check box button my procedd buton is not enabling. If i press second item or second check box item only its enabling.I dont know why ?

Here my both code of did select at indexpath and my check box button action code :

 func proceedbutton(_ sender: AnyObject) {

            let button = sender as? UIButton
            let cell = button?.superview?.superview as? itemsTableViewCell
            let indexPath = tableView.indexPath(for: cell!)
             print(indexPath?.row as Any)


            if selectedIndexPathArray.contains(indexPath! as NSIndexPath) {

                cell?.checkboxBtn.setImage(UIImage(named: "enabled"), for: .normal)
                proceedbutton.isUserInteractionEnabled = true



                let index = selectedIndexPathArray.index(of: indexPath! as NSIndexPath)
                selectedIndexPathArray.remove(at: index!)
            } else {

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

            }
            tableView.reloadData()


        }

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: itemscell, for: indexPath) as! itemsTableViewCell



     cell.checkboxBtn.addTarget(self, action: #selector(proceedbuttonPressed(_:)), for: .touchUpInside)

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

        }

Thanks in advance !




Aucun commentaire:

Enregistrer un commentaire