简体   繁体   English

将tableview选择的数据存储到具有键值的数组中

[英]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 我是swift的新手,并且我已经创建了tableview select all rows功能,所以select deselect对我来说很好用,但是现在我想要在tableview中选择数据,因此我尝试使用Encodable创建结构模型,如下所示

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 您可以在didSelect委托中创建类似var QuotationList = [QuotationListDataModel]()的数组,您可以将模型添加到列表中,例如

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. 无论如何,您都需要通过ID来获取项目...更好,更快的解决方案是使用所选单元格的索引路径,并将其从数据源中提取出来。

EDIT: as stated by Joakim Danielson, add the desired CodingKeys. 编辑:如Joakim Danielson所述,添加所需的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? 需要考虑一些问题,为什么所有类型都是String类型的属性,为什么都是可选的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 存储TableView选定的行 - Store TableView selected Row 如何在TableView中以可变数组存储多个选定的行项目 - How to store multiple selected row items in mutable array in tableview 比较数组和选定值的列表,并根据索引将其显示在tableview单元格中 - Comparing an list of array and selected value and displaying it in tableview cell based on index 如何从TableView单元格收集多个动态值并将其存储在数组中 - How to collect the multiple dynamic value and store in array from tableview cells 将表视图中的数据存储到NSUserDefaults - Store data in tableview to NSUserDefaults 使用数组数据更新 TableView - Update TableView with Array data 如何在iOS的Tableview第一节中设置数组数据的第一个值,在Tableview的第二节中设置数组的数据的剩余值? - How to Set a array data First value in Tableview First Section and remaining array data value in Second Section of tableview in iOS? 如何查询我的数据并将其按类别存储在数组中,以便在TableView中按类别显示它们? - How can I query my data & store them by category in an array so as to display them by category in a TableView? 将tableview所选数据共享到Whats App - share tableview selected data to whats app 根据选择的TableView单元格加载数据 - Load Data depending on what TableView Cell was selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM