无法删除swift中取消选择的行阵列值的tableview

发布于 2025-01-27 00:44:24 字数 2119 浏览 3 评论 0原文

我正在使用TableView使用按钮进行多行选择,并使用Checkmark。使用此代码可以在arrselectedRows中添加所有选定的行ID,并且能够在取消选择时删除....但是在这里,我在arrsubcat中添加所有选定的行JSON标题,而不是能够删除

代码:在此代码arrsubcat是字符串数组。在此数组中,我正在添加选定的行JSON标题.. 这里问题是,如果我是我取消选择该行,然后无法从arrsubcat array artersubcat 删除取消指定的行标题,

如果我尝试这样的arrsubcat.remove(at :( sender.tag))

然后错误:

致命错误:索引超出范围

如何解决此问题。请执行指南

 var arrSelectedRows:[Int] = []
 var arrSubCat:[String] = []

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return getSubCategory?.result?.sub_categories?.count ?? 0
}
var removeIndex: Int?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 
 let cell = tableView.dequeueReusableCell(withIdentifier: "EditLangTableVIewCell", for: indexPath) as! EditLangTableVIewCell

 let id = getSubCategory?.result?.sub_categories?[indexPath.row].id ?? 0

 if arrSelectedRows.contains(id){
     cell.langSelectButn.setImage(UIImage(systemName: "checkmark"), for: .normal)

 }else{
     cell.langSelectButn.setImage(UIImage(named: "checkbox_inactive"), for: .normal)
}
 cell.langSelectButn.tag = id
 cell.langSelectButn.addTarget(self, action: #selector(checkBoxSelection(_:)), for: .touchUpInside)
return cell
}

@objc func checkBoxSelection(_ sender:UIButton)
{
print(sender.tag)
if self.arrSelectedRows.contains(sender.tag){
    self.arrSelectedRows.remove(at: self.arrSelectedRows.firstIndex(of: sender.tag)!)
    arrSubCat.remove(at: (sender.tag))

}else{
    
    self.arrSelectedRows.append(sender.tag)
    print("arrayof selected row ids \(arrSelectedRows)")
    if let selectedSubcat = getSubCategory?.result?.sub_categories{
        for singleSubcat in selectedSubcat{
            if sender.tag == singleSubcat.id{
            arrSubCat.append(singleSubcat.title ?? "")
            }
        }
    }
    print("array of subcat title \(arrSubCat)")
}
self.tableView.reloadData()

}

I am using tableview for multiple row selection with checkmark using button. with this code able to add all selected row ids in arrSelectedRows and able to remove when deselect.... but here i am adding all selected row JSON title in arrSubCat and not able to remove

code: in this code arrSubCat is array of strings.. in this array i am adding selected row JSON title.. here the issue is if i deselect the row then unable to remove deselected row title from arrSubCat array

if i try like this arrSubCat.remove(at: (sender.tag))

then error:

Fatal error: Index out of range

how to solve this issue.. please do guide

 var arrSelectedRows:[Int] = []
 var arrSubCat:[String] = []

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return getSubCategory?.result?.sub_categories?.count ?? 0
}
var removeIndex: Int?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 
 let cell = tableView.dequeueReusableCell(withIdentifier: "EditLangTableVIewCell", for: indexPath) as! EditLangTableVIewCell

 let id = getSubCategory?.result?.sub_categories?[indexPath.row].id ?? 0

 if arrSelectedRows.contains(id){
     cell.langSelectButn.setImage(UIImage(systemName: "checkmark"), for: .normal)

 }else{
     cell.langSelectButn.setImage(UIImage(named: "checkbox_inactive"), for: .normal)
}
 cell.langSelectButn.tag = id
 cell.langSelectButn.addTarget(self, action: #selector(checkBoxSelection(_:)), for: .touchUpInside)
return cell
}

@objc func checkBoxSelection(_ sender:UIButton)
{
print(sender.tag)
if self.arrSelectedRows.contains(sender.tag){
    self.arrSelectedRows.remove(at: self.arrSelectedRows.firstIndex(of: sender.tag)!)
    arrSubCat.remove(at: (sender.tag))

}else{
    
    self.arrSelectedRows.append(sender.tag)
    print("arrayof selected row ids \(arrSelectedRows)")
    if let selectedSubcat = getSubCategory?.result?.sub_categories{
        for singleSubcat in selectedSubcat{
            if sender.tag == singleSubcat.id{
            arrSubCat.append(singleSubcat.title ?? "")
            }
        }
    }
    print("array of subcat title \(arrSubCat)")
}
self.tableView.reloadData()

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昇り龍 2025-02-03 00:44:24

修改checkboxselection()的代码,如下

if self.arrSelectedRows.contains(sender.tag){
    if let selectedIndex = self.arrSelectedRows.firstIndex(of: sender.tag) {
        self.arrSelectedRows.remove(at: selectedIndex)
        arrSubCat.remove(at: selectedIndex)
    }
}

Modify the code of checkBoxSelection() method like below

if self.arrSelectedRows.contains(sender.tag){
    if let selectedIndex = self.arrSelectedRows.firstIndex(of: sender.tag) {
        self.arrSelectedRows.remove(at: selectedIndex)
        arrSubCat.remove(at: selectedIndex)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文