Как мы можем отменить выбор строки, когда мы выключаем - PullRequest
0 голосов
/ 11 мая 2019

У меня есть таблица. В ячейке таблицы у меня есть метка и переключатель. Здесь я хочу отменить выбор строки, когда переключатель выключен.

Вот мой код:

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

enter image description here

1 Ответ

1 голос
/ 11 мая 2019

Не выбирайте и не снимайте флажок ячейки при нажатии переключателя. Просто сохраните indexPath.row выбранных ключей и перезагрузите таблицу.

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