繁体   English   中英

复选框已选中且未选中未在我的表格视图中保留

[英]Check box check and unchecked not persisting in my table view

我的任务是尝试在popup viewcontroller check box显示带有check box按钮的tableView

每当用户单击button它都会更改选中和uncheck但是当我单击“完成” button时,我需要做的事情是必须保持用户checked ,如果单击“ cancel则应该uncheck (在单击“取消”之前用户已选中)。 我需要了解并解决此任务。

选中和取消选中弹出窗口

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:MyCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCell
    cell.myCellLabel.text = self.animals[indexPath.row]

    if selectedRows.contains(indexPath)
    {
        cell.checkBox.setImage(UIImage(named:"check.png"), for: .normal)
    }else{
        cell.checkBox.setImage(UIImage(named:"uncheck.png"), for: .normal)
    }
        cell.checkBox.tag = indexPath.row
        cell.checkBox.addTarget(self, action: #selector(checkBoxSelection(_:)), for: .touchUpInside)
        return cell
}

// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    guard let cell = tableView.cellForRow(at: indexPath) as? MyCustomCell else {
        return
    }

    if self.selectedRows.contains(indexPath) {
        self.selectedRows.remove(at: self.selectedRows.index(of: indexPath)!)
        cell.checkBox.setImage(UIImage(named:"uncheck.png"), for: .normal)
    } else {
        self.selectedRows.append(indexPath)
        cell.checkBox.setImage(UIImage(named:"check.png"), for: .normal)
        let indexPath = tableView.indexPathForSelectedRow //optional, to get from any UIButton for example
        let currentCell = tableView.cellForRow(at: indexPath!) as! MyCustomCell
        }
    }

@IBAction func cancelPopup(_ sender: Any) {
    self.removeAnimate()
}

@IBAction func donePopUp(_ sender: AnyObject) {
    self.removeAnimate()
}

您可以考虑通过设置acessoryType使用基本的taleview单元。 将复选框和标签全部放在左侧似乎使UX效果不佳

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "DefectCell", for: indexPath)

    let category = defectSet [indexPath.section]
    let item = category.items[indexPath.row]

    cell.textLabel?.text = item.name
    cell.selectionStyle = .none

    let isFound = User.shared.selectedDefect.filter{ $0.id == item.id }.count > 0

    if (isFound)
    {
        cell.accessoryType = .checkmark
    }
    else
    {
        cell.accessoryType = .none
    }
    return cell
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM