gpt4 book ai didi

ios - 在 indexPath 的 didselectrow 上选择取消选择行时崩溃

转载 作者:行者123 更新时间:2023-11-28 13:28:37 25 4
gpt4 key购买 nike

我正在创建我的第一个 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 填充数据,直到现在一切都对我来说很完美

但是在 didselect 上,当我取消选择已经选择的行时,我会崩溃,这是我的 didSelect

代码

但是现在我想在“全选”按钮上选择所有表格 View 的所有行,因此我使用了下面的代码并且它对我来说工作正常这是我的代码

@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
}

所以选择所有适合我的功能

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 到结构。

关于ios - 在 indexPath 的 didselectrow 上选择取消选择行时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57884236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com