ios - 复选框选中和未选中不会保留在我的表格 View 中

标签 ios swift uitableview

我的任务我试图在popup View Controller 内显示带有check box按钮的tableView

每当用户单击按钮时,它都会更改检查和取消检查,但当我单击完成按钮时我需要做的事情我必须保留用户已选中,如果单击取消,则应取消选中(这是用户在单击取消之前检查的)。我需要理解并解决这个任务。

Check and Uncheck popup

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

最佳答案

您可以考虑使用基本的 taleview 单元并设置 acessoryType 。将复选框和标签全部放在左侧似乎会导致用户体验不佳

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
}

关于ios - 复选框选中和未选中不会保留在我的表格 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52765747/

相关文章:

cocoa-touch - 间接使用 NSManagedObject 时发送到已释放实例的消息

swift - 弹出 View : Suggesting users when @ is written (like Instagram and Facebook)

ios - 'UITableViewCell' 没有可见的@interface 声明选择器 'dequeueReusableCellWithIdentifier:'

ios - 如何知道哪个 View Controller 呈现了一个 View Controller

ios - SKShapeNode(路径: path) does not draw

ios - 如何找出坐标之间的距离?

ios - Realm - 列表与结果。速度和尺寸

ios - 行分隔符上的标签 - Swift Tableview - 每小时日历

ios - 具有动态 subview swift 的 UITableViewCell 高度

ios - UITextView 滚动错误 iOS9 xcode 7.1.1