Swift中UITableView全选后如何获取所有行ID?
Hey there! Let's get this sorted out for you. Since you're already handling the UI state for select/deselect all, we just need to add logic to collect all the ids from your QuotationListModel data. We'll use Swift's native arrays instead of NSMutableArray—they're safer and more idiomatic for Swift development.
Step 1: Add a Selected IDs Array
First, declare an array to keep track of selected IDs (this can be a class-level variable, just like your isSelectAll flag):
var selectedIds: [String] = [] // Make sure you also have your data source array defined (you must have this from JSON parsing) var quotationList: [QuotationListModel] = []
Step 2: Update the Select All Button Action
Modify your btnSelectAllTapped method to populate or clear the selectedIds array when the button is tapped:
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) // Collect all IDs from the data source in one line self.selectedIds = quotationList.map { $0.id } 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) // Clear all selected IDs self.selectedIds.removeAll() self.btnShare.isHidden = true self.tblListView.reloadData() } }
Step 3: (Optional) Support Individual Row Selection
If you plan to let users select/deselect individual rows later, update your cellForRowAt method to check both the isSelectAll flag and the selectedIds array. This makes your code flexible for future enhancements:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellIdentifier", for: indexPath) as! YourCustomCell let model = quotationList[indexPath.row] // Check if the row is selected (either via select all or individual selection) let isRowSelected = isSelectAll || selectedIds.contains(model.id) if isRowSelected { 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 } // Don't forget to configure other cell elements with the model data return cell }
Why Avoid NSMutableArray?
Swift's native Array is a value type, which means it's safer from unexpected side effects. Plus, Swift provides powerful higher-order functions like map (used above to extract all IDs in one line) that make data manipulation far more concise than using NSMutableArray methods.
Now whenever you tap "Select All", the selectedIds array will hold all the IDs from your quotationList data, ready to use for whatever action you need (like sharing, as your btnShare suggests).
内容的提问来源于stack exchange,提问作者Vishal

