0

I am creating Swift app and I have to select all UITableView rows on one button click. I achieved this functionality, but when I try to implement Web services in my app so that once all rows are selected the id of all rows will be available. The code that I have implemented is given below:

This is my JSON response

{
  "message" : "Quation List",
  "success" : "1",
  "quation_list" : [
    {
      "fk_customer_id" : "1",
      "total_price" : "1230",
      "margin" : "30",
      "freight" : "20",
      "created_date" : "2019-09-06 14:14:41",
      "id" : "1",
      "created_by" : "8",
      "quantity" : "2",
      "part_number" : "15"
    }
  ]
}

So for this JSON, I have created a model class for parsing as below

struct QuotationListModel {
    var id: String
    var quantity: String
    var margin: String
    var created_date: String
    var part_number: String
    var total_price: String
    var freight: String
    var fk_customer_id: String
}

up to this part, everything is perfect, My button click code for selecting all rows is given below

var isSelectAll = false //Global Variable

@IBAction func btnSelectAllTapped(_ sender: UIButton) {
    if btnSelectAll.titleLabel?.text == "Select All"{
        self.isSelectAll = true
        self.btnSelectAll.setTitle("DeSelect All", for: .normal)
        self.btnSelectAll.backgroundColor = UIColor(red: 119/255, green: 119/255, blue: 119/255, alpha: 1)
        self.tblListView.reloadData()
        self.btnShare.isHidden = false
    } else {
        self.isSelectAll = false
        btnSelectAll.setTitle("Select All", for: .normal)
        btnSelectAll.backgroundColor = UIColor(red: 0/255, green: 175/255, blue: 239/255, alpha: 1)
        self.btnShare.isHidden = true
        self.tblListView.reloadData()
    }
}

and here is code for cellForRowAt IndexPath

if(isSelectAll==true) {
    cell.viewMain.backgroundColor = UIColor(red: 210/255, green: 251/255, blue: 255/255, alpha: 1)
    cell.imgView.isHidden = false
} else {
    cell.viewMain.backgroundColor = UIColor.white
    cell.imgView.isHidden = true
}

so with this code SelectAll DeSelect All work fine for me but how to get all rows id on Select All button click please help me

I got this below answer but in the link provided below, he used NSMutableArray`

Refer the link

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
Vishal
  • 57
  • 1
  • 7
  • If you select all row on your button click then fetch all selected row ids from your array. Try this : `let arrSelectedIds = YOUR_ARRAY.map({ (quotation: QuotationListModel) -> String in quotation.id })` – Kuldeep Sep 09 '19 at 12:55
  • That's nice, you can accept the answer as well. – Kuldeep Sep 09 '19 at 13:00

1 Answers1

0

If you select all row on your button click then fetch all selected row ids from your array. Try this :

let arrSelectedIds = YOUR_ARRAY.map({ (quotation: QuotationListModel) -> String in quotation.id })
Kuldeep
  • 4,466
  • 8
  • 32
  • 59