0

I have tableview with custom check box. here if i click select all button which is outside of tableview then i need to show all tableview rows selected with checkmark

code: tableview cell contains chkImg and chkBtn for row selection. with this code i can select and deselect multiple rows but if i click selectAllBtn i need all rows selected with cell.chkImg.image = UIImage(systemName: "checkmark") and all rows id in arrSelectedRows

how to do that please guid me

 var arrSelectedRows:[Int] = []

@IBAction func selectAllBtn(_ sender: UIButton) {
        tableView.reloadData()
}

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: ServiceListTableViewCell.cellIdentifier, for: indexPath) as! ServiceListTableViewCell

let id = self.serviceList?.result?.services?[indexPath.row].id ?? 0

 if arrSelectedRows.contains(id){
     cell.chkImg.image = UIImage(systemName: "checkmark")
 }else{
     cell.chkImg.image = UIImage(named: "checkbox_inactive")
}
 cell.chkBtn.tag = id
 cell.chkBtn.addTarget(self, action: #selector(checkBoxSelection(_:)), for: .touchUpInside)

return cell
}

@objc func checkBoxSelection(_ sender:UIButton)
{
print(sender.tag)

if self.arrSelectedRows.contains(sender.tag){
    self.arrSelectedRows.remove(at: self.arrSelectedRows.index(of: sender.tag)!)
    print("arrayof selected row ids \(arrSelectedRows)")
}else{
    self.arrSelectedRows.append(sender.tag)
}
self.tableView.reloadData()
}
Json Swift
  • 31
  • 1
  • 7

3 Answers3

1

On your button click, exceute this lines of code.

for i in 0..<self.serviceList?.result?.services.count{
   arrSelectedRows.append(self.serviceList?.result?.services?[i].id)
}
self.tableView.reloadData()
Shabnam Siddiqui
  • 579
  • 4
  • 13
1

You can use Swift map function, by it you get all ids in your selectedArray

@IBAction func selectAllBtn(_ sender: UIButton) {
   let servicesCount = self.serviceList?.result?.services?.count ?? 0
   if servicesCount == self.arrSelectedRows {
       self.arrSelectedRows.removeAll()
   } else {
       self.arrSelectedRows = self.serviceList?.result?.services?.map({$0.id ?? 0})
   }
   tableView.reloadData()
}
  • could you tell me how to deselect all rows when tap again – Json Swift May 05 '22 at 06:38
  • @JsonSwift do you want to deselect all by pressing the same Select All button? – MBT May 05 '22 at 06:42
  • If you are using different button then it can be like ```@IBAction func deselectAllBtn(_ sender: UIButton) { self.arrSelectedRows.removeAll() tableView.reloadData() }``` – MBT May 05 '22 at 06:45
  • @MBT, i need to deselect all by pressing same button(selectAllBtn) – Json Swift May 05 '22 at 07:31
  • 1
    @JsonSwift i have posted an answer as it's a bit long. – MBT May 05 '22 at 07:49
  • @JsonSwift you can check for count of selected array and self.serviceList?.result?.services? count if match then perform removeall operation on selected array – Azruddin Shaikh May 05 '22 at 09:07
  • could u answer this as well https://stackoverflow.com/questions/72162342/unable-to-remove-tableviews-deselected-row-array-value-in-swift – Json Swift May 08 '22 at 16:19
1

@Azruddin Shaikh's answer is correct for this question. I am going to add logic of deselecting all items by pressing the same button.

Firstly, you have to create an IBOutlet of the button

@IBOutlet weak var selectAllButton: UIButton!

Then in IBAction method, change the title of the button depending on the current title of the button.

@IBAction func selectAllBtn(_ sender: UIButton) {
    let titleText = sender.titleLabel?.text ?? ""
    if titleText == "Select All" {
        self.arrSelectedRows = self.serviceList?.result?.services?.map({$0.id ?? 0})
        self.selectAllButton.setTitle("Deselect All", for: .normal)
    } else if titleText == "Deselect All" {
        self.arrSelectedRows.removeAll()
        self.selectAllButton.setTitle("Select All", for: .normal)
    }
    tableView.reloadData()
}
MBT
  • 1,381
  • 1
  • 6
  • 10
  • could u answer this as well https://stackoverflow.com/questions/72366920/unable-to-check-multiple-if-conditions-in-swift – Json Swift May 24 '22 at 17:14