当我在swift中单击tableview的按钮时,如何选择所有行并在tableview中获取其ID
我有带有自定义复选框的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用Swift Map函数,通过它,您可以在选定的array中获得所有ID
You can use Swift map function, by it you get all ids in your selectedArray
在您的按钮点击时,请删除此代码行。
On your button click, exceute this lines of code.
@Azruddin Shaikh的答案是正确的。我将通过按相同的按钮来添加取消选择所有项目的逻辑。
首先,您必须创建按钮的按钮的
iboutlet
,然后在
ibaction
方法中,根据按钮的当前标题更改按钮的标题。@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 buttonThen in
IBAction
method, change the title of the button depending on the current title of the button.