我正在创建 Swift
应用程序,我必须单击一下按钮即可选择所有 UITableView
行。我实现了此功能,但是当我尝试在我的应用程序中实现 Web 服务时,一旦选择了所有行,所有行的 id 都将可用。我实现的代码如下:
这是我的 JSON
响应
{
"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"
}
]
}
因此,对于这个 JSON
,我创建了一个用于解析的模型类,如下所示
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
}
到目前为止,一切都很完美,下面给出了我用于选择所有行的按钮单击代码
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()
}
}
这是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
}
因此,使用此代码 SelectAll
DeSelect All
对我来说工作正常,但如何在“全选”按钮单击上获取所有行 ID,请帮助我
我得到了下面的答案,但在下面提供的链接中,他使用了 NSMutableArray`
最佳答案
如果您选择按钮上的所有行,请单击然后从数组中获取所有选定的行 ID。试试这个:
let arrSelectedIds = YOUR_ARRAY.map({ (quotation: QuotationListModel) -> String in quotation.id })
关于ios - 如何使用json解析选择所有 TableView 行并获取所有行的id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57854319/