无法删除swift中取消选择的行阵列值的tableview
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
修改
checkboxselection()
的代码,如下Modify the code of
checkBoxSelection()
method like below