5

i have added data in table view and i have manually added "select all" option in to list at first position now when user select first option which is select all then all item in to list should be selected and deselected when choose same. i have tried code below but its not working so can any one help me to solve this

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = ObjTableview.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SelectUserCell
    for i in 0 ..< self.getStudentName.count {
        cell.btnCheckbox.setImage(UIImage(named: "selectedItem"), for: .normal)
        print (i) //i will increment up one with each iteration of the for loop

    }

}
var unchecked = true
 @IBAction func btnCheckBoxClick(_ sender: Any) {

if unchecked == true {
           //(sender as AnyObject).setImage(UIImage(named: "selectedItem"), for: .normal)
            cell?.btnCheckbox.setImage(UIImage(named: "selectedItem"), for: .normal)
            //unchecked = false
            let cello = ObjTableview.cellForRow(at: indexPath!)
            print(cello!)
            ObjTableview.reloadData()
        }else
        {
            //(sender as AnyObject).setImage(UIImage(named: "unSelectedItem"), for: .normal)
            cell?.btnCheckbox.setImage(UIImage(named: "unSelectedItem"), for: .normal)
           // unchecked = true
        }
}
Jayprakash Singh
  • 1,343
  • 3
  • 15
  • 28
  • In cell for row you are setting only `UIImage(named: "selectedItem"` and why for loop ? only last value of array been applied , Other problem i can see is `btnCheckBoxClick` where you are updating `unchecked` value – Prashant Tukadiya Sep 25 '17 at 06:17
  • Make your logic on Array. once data load in array put all zeros and when you select any cell change that index value to 1. and when u selectAll in Array put all 1. and when u unselectAll put All zeros in array. –  Sep 25 '17 at 06:18
  • **Never ever** dequeue a table view cell outside of `cellForRow...`. That's a fatal mistake. – vadian Sep 25 '17 at 07:23

1 Answers1

0

Jayprakash, You are almost there. You need to modify some lines -

Here is your modified code snippet

var unchecked:Bool = true

@IBAction func btnCheckBoxClick(_ sender: Any) {

    if(unchecked){

        unchecked = false
    }
    else{

        unchecked = true
    }


    ObjTableview.reloadData()


}



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if(indexPath.row == 0){

            btnCheckBoxClick(sender: UIButton())

      }

 }


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

        let cell : SelectUserCell!
        cell = tableView .dequeueReusableCell(withIdentifier: "SelectUserCell", for: indexPath) as! SelectUserCell
        cell.selectionStyle = UITableViewCellSelectionStyle.none


       if(unchecked){

              cell.btnCheckbox.setImage(UIImage(named: "unSelectedItem"), for: .normal)

         }
       else{

              cell.btnCheckbox.setImage(UIImage(named: "selectedItem"), for: .normal)
         }


   // Do your stuff here

   return cell
}

Hop it will simplify your code structure.

Amir Khan
  • 1,318
  • 1
  • 14
  • 39