【问题标题】:How can we deselect the row when we Off the switch关闭开关时如何取消选择行
【发布时间】:2019-05-10 22:22:00
【问题描述】:

我有一个表格视图。在 tableview 单元格中,我有一个标签和开关。这里我想在开关关闭时取消选择该行。

这是我的代码:

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

    cell.tapSwitch.tag = indexPath.row
    cell.businessLabel.text = labelArray[indexPath.row]
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}

【问题讨论】:

  • 未选择任何行。没有什么可以取消的。
  • 我该如何选择请建议我@matt
  • 我可以使用委托
  • 该行在用户点击时被选中。然后你在委托中取消选择。我不明白应该是什么问题。
  • 当 switch isOn = true 时该行应该是正常的,但是当 switch isOn = false 它应该取消选择该行...我正在尝试这个功能...请帮助我@matt

标签: ios swift uitableview uiswitch


【解决方案1】:

点击开关时不要选择/取消选择单元格。只需存储所选开关的 indexPath.row 并重新加载 tableview。

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate   {
    @IBOutlet weak var tableView: UITableView!

    let labelArray = ["Employees", "Break Time Setup", "Employee Timeoff", "Reports", "Messages"]
    var selectedIndexPaths = [Int]()
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return labelArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! Cell
        cell.selectionStyle = .none
        cell.tapSwitch.isOn = selectedIndexPaths.contains(indexPath.row)
        cell.tapSwitch.tag = indexPath.row
        cell.tapSwitch.addTarget(self, action: #selector(tapSwitchAction(_:)), for: .valueChanged)
        cell.businessLabel.text = labelArray[indexPath.row]
        return cell
    }
    @objc func tapSwitchAction(_ sender: UISwitch) {
        if sender.isOn {
            selectedIndexPaths.append(sender.tag)
        } else {
            selectedIndexPaths.removeAll { $0 == sender.tag }
        }
        tableView.reloadData()
    }
}

然后你可以像这样在任何地方获取选定的行值

@objc func getSelectedValues() {
    let selectedLabelArray = labelArray.enumerated().filter { selectedIndexPaths.contains($0.offset) }
    print(selectedLabelArray)
}

更新

选项 1

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if selectedIndexPaths.contains(indexPath.row) {
        selectedIndexPaths.removeAll { $0 == indexPath.row }
    } else {
        selectedIndexPaths.append(indexPath.row)
    }
    tableView.reloadData()
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    //do nothing
}

选项 2

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) as? BM_MyBusinessTableViewCell {
        cell.tapSwitch.isOn = !cell.tapSwitch.isOn
        tapSwitchAction(cell.tapSwitch)
    }
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) as? BM_MyBusinessTableViewCell {
        cell.tapSwitch.isOn = !cell.tapSwitch.isOn
        tapSwitchAction(cell.tapSwitch)
    }
}

【讨论】:

  • 一切正常,但唯一的问题是无法取消选择是否有任何解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
相关资源
最近更新 更多