Agaram
Agaram

Reputation: 93

Multiple checkmark in Tableview in swift3

How to do the multiple checkmark in tableview. I need to select the multiple checkmark in tableview and what are the checkmarks I need to select to place the multiple values in label. Example player1,player2,player3 in label

here is my code

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return TypeOfAccountArray.count
    }



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell


        let cell:TypeofAccountCell=tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TypeofAccountCell

        cell.Uertype_lbl.text=TypeOfAccountArray[indexPath.row]

        cell.selectionStyle = UITableViewCellSelectionStyle.none;
        cell.Uertype_lbl.font = UIFont(name:"Roboto-Regular", size:13)
        cell.Uertype_lbl.adjustsFontSizeToFitWidth = true


        if (selectedIndex == indexPath as NSIndexPath?) {
            cell.checkmarkbtn.setImage(UIImage(named: "checkmark.png"),for:UIControlState.normal)
        } else {
            cell.checkmarkbtn.setImage(UIImage(named: "uncheckmark.png"),for:UIControlState.normal)
        }


        // Configure the cell...

        return cell
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)
        let row = indexPath.row
        print(TypeOfAccountArray[row])
        selectedIndex = indexPath as NSIndexPath?
        self.Type_of_account_txt.text = (TypeOfAccountArray[row])
        self.Type_account_view.isHidden = true
        tableView.reloadData()
    }

Upvotes: 2

Views: 935

Answers (2)

Francis F
Francis F

Reputation: 3295

Change your selectedindex to hold array of index path var selectedIndexes = [IndexPath](), on your cell xib, set your checkmark image on button selected stated and uncheckmark image on normal status and use the below code.

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return TypeOfAccountArray.count
    }


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

        let cell:TypeofAccountCell=tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TypeofAccountCell

        cell.Uertype_lbl.text=TypeOfAccountArray[indexPath.row]

        cell.selectionStyle = UITableViewCellSelectionStyle.none;
        cell.Uertype_lbl.font = UIFont(name:"Roboto-Regular", size:13)
        cell.Uertype_lbl.adjustsFontSizeToFitWidth = true



        // Configure the cell...

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)



        let cell:TypeofAccountCell=tableView.cellForRow(at: indexPath) as! TypeofAccountCell

        if selectedIndexes.contains(indexPath)
        {

            cell.checkmarkbtn.isSelected = false

            if let index = selectedIndexes.index(of: indexPath) {
                selectedIndexes.remove(at: index)
            }
        }
        else
        {
            cell.checkmarkbtn.isSelected = true

            selectedIndexes.append(indexPath)

        }
    }



    self.Type_of_account_txt.text = ""
    for element in selectedIndexes
    {
        self.Type_of_account_txt.text = (self.Type_of_account_txt.text ?? "") + "\(TypeOfAccountArray[element.row]) ,"


    }
    if (selectedIndexes.count > 0)
    {
        self.Type_of_account_txt.text = self.Type_of_account_txt.text?.substring(to: (self.Type_of_account_txt.text?.index(before: (self.Type_of_account_txt.text?.endIndex)!))!)
    }
}

Upvotes: 2

KKRocks
KKRocks

Reputation: 8322

you need to follow this step :

  1. In didSelectRowAt, you need to add and remove indexpath in array for multiple checkmark.

  2. Now , in cellForRowAtIndexPath you need to check that current

    indexPath consist in array .

     if (![arrIndexPath containsObject: indexPath]) {
    
            //  do something
    
        cell.checkmarkbtn.setImage(UIImage(named: "checkmark.png"),for:UIControlState.normal)
    
                 }
    

Upvotes: 0

Related Questions