如何隐藏来自单元格的切换?

时间:2017-08-18 07:08:37

标签: swift

当按钮标题编辑变为完成时,如何隐藏从单元格的切换?

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing,animated:animated)

    if self.isEditing{

        self.editButtonItem.title = "Done"
        tableview.setEditing(true, animated: true)
        tableview.reloadData()

    }
    else{
        self.editButtonItem.title = "Edit"
        tableview.setEditing(false, animated: false)

    }


}

2 个答案:

答案 0 :(得分:0)

在setEditing方法中,您可以通过这种方式访问​​所有可见行

tableView.visibleCells.forEach{ cell in
       //define your custom cell class
       If let customCell = cell as? YourCustomCellCalss{
      //call here the method to show/hide the switch or whatever you have for hiding it
     customCell.hideSwitch()
     }
}

但是它只会改变可见的行,所以你需要在cellForRowAt方法中为self.isEditing添加一个检查,以便滚动显示/隐藏开关后将出列的所有行

如果我的代码与您的自定义单元格实现不同,请在问题中添加更多代码

让我知道你是否需要一些东西

答案 1 :(得分:0)

您可以使用开关的隐藏属性。像这样。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "democell", for: indexPath) as! DemoViewCell
        cell.selectionStyle = .none
        if self.isEditing{
          cell.yourSwitch.isHidden = false
    }
    else{
        cell.yourSwitch.isHidden = false
    }


        return cell
    }