Swift 如果在tableview中选中复选框,如何保存所选阵列
目前,我只得到一个值,如果我选择了多个复选按钮,如何获得该值?请修复代码的这一部分:Swift 如果在tableview中选中复选框,如何保存所选阵列,swift,uitableview,Swift,Uitableview,目前,我只得到一个值,如果我选择了多个复选按钮,如何获得该值?请修复代码的这一部分: func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arraylist.count } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGF
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arraylist.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50.0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = PopupCellTableViewCell()
cell = (tableView.dequeueReusableCell(withIdentifier: "advnccell", for: indexPath) as? PopupCellTableViewCell)!
cell.bgView = Utilities().viewborder(vw: cell.bgView )
let dict = arraylist[indexPath.row] as! NSDictionary
let cd = dict.value(forKey: "Code") as? String ?? ""
let desc = dict.value(forKey: "Description") as? String ?? ""
cell.tittleLbl.text = cd + " - " + desc
if selectedRows.contains(indexPath)
{
cell.checkBtn.setImage(UIImage(named:"check"), for: .normal)
}
else
{
cell.checkBtn.setImage(UIImage(named:"uncheck"), for: .normal)
}
if (cell.checkBtn.currentImage?.isEqual(UIImage(named: "check")))!
{
UserDefaults.standard.set(cell.tittleLbl.text, forKey: "tittle")
}
cell.btnAction.tag = indexPath.row
cell.btnAction.addTarget(self, action: #selector(checkBoxSelection(_:)), for: .touchUpInside)
return cell
}
// checkbox selection
@objc func checkBoxSelection(_ sender:UIButton)
{
let selectedIndexPath = IndexPath(row: sender.tag, section: 0)
if self.selectedRows.contains(selectedIndexPath)
{
self.selectedRows.remove(at: self.selectedRows.index(of: selectedIndexPath)!)
}
else
{
self.selectedRows.append(selectedIndexPath)
}
self.tableVw.reloadData()
}
提供您迄今为止尝试过的代码?与其使用额外的
selectedRows
数组,不如将selectedRows
信息作为Bool
值放入数据模型中,这样效率更高,并且避免了丑陋的UIImage
比较。和往常一样,不要使用NSArray/NSDictionary
和值(Swift中的forKey
。你能清楚地告诉我吗?bcz i am Fresh@Vadian使用自定义结构作为数据模型,而不是字典,并添加Bool
属性selected
。使用该属性设置适当的按钮图像。感谢它的工作,但我需要多个选定数据。现在我只得到一个字符串,我得到的是空数组和我要选定的数组@Carpsen@MeMouli“空数组”是什么意思?什么类型的arraylist
?对于selectedRows
if selectedRows.contains(indexPath)
{
cell.checkBtn.setImage(UIImage(named:"check"), for: .normal)
UserDefaults.standard.set(cell.tittleLbl.text, forKey: "tittle")
}
else
{
cell.checkBtn.setImage(UIImage(named:"uncheck"), for: .normal)
}