简体   繁体   中英

store tableview selected data into array with key value

i am new to swift and and i have created tableview select all rows functionality so select deselect works fine for me but now i want data which is selected in tableview so i tried to create struct model with Encodable like below

class QuotationListDataModel: Encodable{
    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?

    init(id: String?,quantity: String?,margin: String?,created_date: String?,part_number: String,total_price: String,freight: String,fk_customer_id: String) {
        self.id = id
        self.quantity = quantity
        self.margin = margin
        self.created_date = created_date
        self.part_number = part_number
        self.total_price = total_price
        self.freight = freight
        self.fk_customer_id = fk_customer_id
    }
}

and i want out put like below

[
  {
   margin: 20,
   quantity: 10
   part_number: 15
   total_price: 1500
   freight: 100
  },
  {
   margin: 20,
   quantity: 10
   part_number: 15
   total_price: 1500
   freight: 100
  }
]


@IBAction func btnSelectAllTapped(_ sender: UIButton) {
    if btnSelectAll.titleLabel?.text == "Select All"{
        self.btnSelectAll.setTitle("DeSelect All", for: .normal)
        self.btnSelectAll.backgroundColor = UIColor(red: 119/255, green: 119/255, blue: 119/255, alpha: 1)
        self.btnShare.isHidden = false
        self.arrSelectedIds = quotationSeelctedData.map({ (quotation: QuotationListDataModel) -> String in quotation.id! }) 
         //Here when user select all i want all data into array
        self.tblListView.reloadData()
    }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.arrSelectedIds.removeAll()
        print(arrSelectedIds)
        self.tblListView.reloadData()
    }
}

so i want selected data like this can anyone please help me to solve it out

you could create an array like var QuotationList = [QuotationListDataModel]() in didSelect delegate you could add the model to your list like

QuotationList = allDataArray[indexPath.row]

selected data will be added to your array

Try this:

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted

do {
    let jsonData = try encoder.encode(MYARRAY)
    if let jsonString = String.init(data: jsonData, encoding: .utf8) {
        // That's your JSON string...
        print(jsonString)
    }
} catch {
    print("the encoding failed")
}

Anyway you need to fetch your items by ID... a better and faster solution would be to use the indexpath of the selected cells and extract them from your datasource.

EDIT: as stated by Joakim Danielson, add the desired CodingKeys.

To encode your rows you need to add an enum containing the properties you want to be part of the encoded data to your class

  enum CodingKeys: String, CodingKey {
      case margin, quantity, part_number, total_price, freight
  }

Then you can encode it like this

do {
    let data = try JSONEncoder().encode(arr)                                 
} catch {
    print(error)
}

Some questions to consider, why are all properties of type String and why are all optional?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM