当我在swift中单击tableview的按钮时,如何选择所有行并在tableview中获取其ID

发布于 2025-01-26 06:52:02 字数 1480 浏览 5 评论 0原文

我有带有自定义复选框的TableView。在此处,如果我单击选择所有按钮,该按钮在表观视图之外,那么我需要显示与Checkmark

代码选择的所有tableView行: TableView单元格包含CHKIMG和CHKBTN,以进行行选择。使用此代码,我可以选择并取消选择多个行,但是如果我单击selectallBtn我需要选择所有的行,请使用cell.chkimg.image = uiimage(systemName:checkmark:“ checkmark”)和arrselectedRows中的所有行ID

如何做到这一点,请给我指导我

 var arrSelectedRows:[Int] = []

@IBAction func selectAllBtn(_ sender: UIButton) {
        tableView.reloadData()
}

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: ServiceListTableViewCell.cellIdentifier, for: indexPath) as! ServiceListTableViewCell

let id = self.serviceList?.result?.services?[indexPath.row].id ?? 0

 if arrSelectedRows.contains(id){
     cell.chkImg.image = UIImage(systemName: "checkmark")
 }else{
     cell.chkImg.image = UIImage(named: "checkbox_inactive")
}
 cell.chkBtn.tag = id
 cell.chkBtn.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.index(of: sender.tag)!)
    print("arrayof selected row ids \(arrSelectedRows)")
}else{
    self.arrSelectedRows.append(sender.tag)
}
self.tableView.reloadData()
}

I have tableview with custom check box. here if i click select all button which is outside of tableview then i need to show all tableview rows selected with checkmark

code: tableview cell contains chkImg and chkBtn for row selection. with this code i can select and deselect multiple rows but if i click selectAllBtn i need all rows selected with cell.chkImg.image = UIImage(systemName: "checkmark") and all rows id in arrSelectedRows

how to do that please guid me

 var arrSelectedRows:[Int] = []

@IBAction func selectAllBtn(_ sender: UIButton) {
        tableView.reloadData()
}

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: ServiceListTableViewCell.cellIdentifier, for: indexPath) as! ServiceListTableViewCell

let id = self.serviceList?.result?.services?[indexPath.row].id ?? 0

 if arrSelectedRows.contains(id){
     cell.chkImg.image = UIImage(systemName: "checkmark")
 }else{
     cell.chkImg.image = UIImage(named: "checkbox_inactive")
}
 cell.chkBtn.tag = id
 cell.chkBtn.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.index(of: sender.tag)!)
    print("arrayof selected row ids \(arrSelectedRows)")
}else{
    self.arrSelectedRows.append(sender.tag)
}
self.tableView.reloadData()
}

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

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

发布评论

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

评论(3

愿与i 2025-02-02 06:52:02

您可以使用Swift Map函数,通过它,您可以在选定的array中获得所有ID

@IBAction func selectAllBtn(_ sender: UIButton) {
   let servicesCount = self.serviceList?.result?.services?.count ?? 0
   if servicesCount == self.arrSelectedRows {
       self.arrSelectedRows.removeAll()
   } else {
       self.arrSelectedRows = self.serviceList?.result?.services?.map({$0.id ?? 0})
   }
   tableView.reloadData()
}

You can use Swift map function, by it you get all ids in your selectedArray

@IBAction func selectAllBtn(_ sender: UIButton) {
   let servicesCount = self.serviceList?.result?.services?.count ?? 0
   if servicesCount == self.arrSelectedRows {
       self.arrSelectedRows.removeAll()
   } else {
       self.arrSelectedRows = self.serviceList?.result?.services?.map({$0.id ?? 0})
   }
   tableView.reloadData()
}
灯角 2025-02-02 06:52:02

在您的按钮点击时,请删除此代码行。

for i in 0..<self.serviceList?.result?.services.count{
   arrSelectedRows.append(self.serviceList?.result?.services?[i].id)
}
self.tableView.reloadData()

On your button click, exceute this lines of code.

for i in 0..<self.serviceList?.result?.services.count{
   arrSelectedRows.append(self.serviceList?.result?.services?[i].id)
}
self.tableView.reloadData()
錯遇了你 2025-02-02 06:52:02

@Azruddin Shaikh的答案是正确的。我将通过按相同的按钮来添加取消选择所有项目的逻辑。

首先,您必须创建按钮的按钮的iboutlet

@IBOutlet weak var selectAllButton: UIButton!

,然后在ibaction方法中,根据按钮的当前标题更改按钮的标题。

@IBAction func selectAllBtn(_ sender: UIButton) {
    let titleText = sender.titleLabel?.text ?? ""
    if titleText == "Select All" {
        self.arrSelectedRows = self.serviceList?.result?.services?.map({$0.id ?? 0})
        self.selectAllButton.setTitle("Deselect All", for: .normal)
    } else if titleText == "Deselect All" {
        self.arrSelectedRows.removeAll()
        self.selectAllButton.setTitle("Select All", for: .normal)
    }
    tableView.reloadData()
}

@Azruddin Shaikh's answer is correct for this question. I am going to add logic of deselecting all items by pressing the same button.

Firstly, you have to create an IBOutlet of the button

@IBOutlet weak var selectAllButton: UIButton!

Then in IBAction method, change the title of the button depending on the current title of the button.

@IBAction func selectAllBtn(_ sender: UIButton) {
    let titleText = sender.titleLabel?.text ?? ""
    if titleText == "Select All" {
        self.arrSelectedRows = self.serviceList?.result?.services?.map({$0.id ?? 0})
        self.selectAllButton.setTitle("Deselect All", for: .normal)
    } else if titleText == "Deselect All" {
        self.arrSelectedRows.removeAll()
        self.selectAllButton.setTitle("Select All", for: .normal)
    }
    tableView.reloadData()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文