Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如果在tableview中选中复选框,如何保存所选阵列_Swift_Uitableview - Fatal编程技术网

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)
}