while select deselect row on didselectrow at indexPath 崩溃

while select deselect row on didselectrow at indexPath getting crash

我正在创建我的第一个 Swift 应用程序,我正在将 JOSN 数据填充到 UITableview 并使用 struct 模型让我向您展示我的填充 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
}

在我的 ViewController 中,我已经声明了如下数组

var quotationData = [QuotationListModel]()
var arrSelectedIds = [String]()

下面是我的API调用函数

func quotationListAPI(){
    let preferences = UserDefaults.standard
    let uid = "u_id"
    let acTkn = "acc_tkn"

    let u_ID = preferences.object(forKey: uid)
    let A_Token = preferences.object(forKey: acTkn)

    let params = ["user_id": u_ID!, "access_token": A_Token!]
    print(params)
    self.viewMainSpinner.isHidden = false
    self.viewInnerSpinner.startAnimating()
    Alamofire.request(quatationlist, method: .post, parameters: params).responseJSON(completionHandler: {(response) in
        switch response.result{
        case.success(let value):
            let json  = JSON(value)
            print(json)
            let data = json["quation_list"]
            print(data)
            if data == []{
                self.viewMainSpinner.isHidden = true
                self.viewInnerSpinner.stopAnimating()
            }else{
                data.array?.forEach({ (qList) in
                    let q_list = QuotationListModel(id: qList["id"].stringValue, quantity: qList["quantity"].stringValue, margin: qList["margin"].stringValue, created_date: qList["created_date"].stringValue, part_number: qList["part_number"].stringValue, total_price: qList["total_price"].stringValue, freight: qList["freight"].stringValue, fk_customer_id: qList["fk_customer_id"].stringValue)
                    self.quotationData.append(q_list)
                })
                self.tblListView.reloadData()
                self.viewMainSpinner.isHidden = true
                self.viewInnerSpinner.stopAnimating()
            }
        case.failure(let error):
            print(error.localizedDescription)
            self.viewMainSpinner.isHidden = true
            self.viewInnerSpinner.stopAnimating()
        }

    })
}

所以我用这个数组填充了从 cellForRowAtIndexPath 到现在的所有数据对我来说都很完美

但是在 select 上,当我 deselect 行已经 selected 然后我崩溃了这是我的代码 didSelect

但现在我想要 select Select 所有表格视图的所有行点击所有按钮所以我使用了下面的代码并且它对我来说工作正常这是我的代码

@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: QuotationListModel) -> String in quotation.id })
        print(arrSelectedIds)
        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()
    }
}

这是我的 cellForRowAt indexPath

代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! QuotationTableViewCell
    let id = quotationSeelctedData[indexPath.row].id
    if self.arrSelectedIds.contains(id){
        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
    }

    cell.lblPartNumber.text = quotationData[indexPath.row].part_number
    cell.llbQuantity.text = quotationData[indexPath.row].quantity
    cell.lblFreight.text = quotationData[indexPath.row].freight
    cell.lblMargin.text = quotationData[indexPath.row].margin
    cell.lblTotal.text = quotationData[indexPath.row].total_price
    cell.selectionStyle = .none
    return cell
}

所以 select 所有功能对我来说都很完美

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(indexPath.row)
    let id = quotationSeelctedData[indexPath.row].id
    print(id)
    if arrSelectedIds.contains(id){
        self.arrSelectedIds.remove(at: indexPath.row)
        print(self.arrSelectedIds)
        if self.arrSelectedIds.count == 0{
            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()
    }else{
        self.arrSelectedIds.append(id)
        print(self.arrSelectedIds)
        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.tblListView.reloadData()
    }
}

所以请任何人帮助我解决这个问题提前谢谢

发生错误是因为您为选定的 ID 使用了额外的数组,而不是将 isSelected 成员添加到结构

索引路径不一定是项目的索引,因为 id 只是附加到数组

替换

if arrSelectedIds.contains(id){
    self.arrSelectedIds.remove(at: indexPath.row)

if let index = arrSelectedIds.index(of: id){
    self.arrSelectedIds.remove(at: index)

然而,删除 arrSelectedIds 并向结构中添加一个成员 isSelected