簡體   English   中英

關閉開關時如何取消選擇行

[英]How can we deselect the row when we Off the switch

我有一張桌子。 在表格單元格中,我有一個標簽和開關。 在這里,我想取消選擇關閉開關時的行。

這是我的代碼:

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

在此處輸入圖片說明

輕按開關時,請勿選擇/取消選擇單元格。 只需存儲所選開關的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)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM