当实现 willDisplay 功能时,UITableViewCell 不会取消选择 [英] UITableViewCell not deselecting when willDisplay function is implemented

查看:11
本文介绍了当实现 willDisplay 功能时,UITableViewCell 不会取消选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView,它显示了几个可供用户选择的可用选项.我想要的是表格始终反映选择的选项,这些选项存储在一个数组中,该数组是与视图控制器的类分开的类的一部分.我试图通过使用 tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 方法在表格加载时显示选定的选项.我遇到的问题是,当我实现此方法时,表加载时数组中的任何选项在按下时都不会取消选择.代码如下:

I have a UITableView which displays several available options for the user to choose from. What I want is for the table to always reflect the options that are selected, which are stored in an array that is part of a class separate from that of the view controller. I am attempting to have the selected options displayed when the table loads by using the tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) method. The problem that I am having is that when I implement this method, any option that is in the array when the table loads does NOT deselect when pressed. Code is as follows:

class Options {
    enum Option : String {
        case option1 = "Option 1"
        case option2 = "Option 2"
        case option3 = "Option 3"
        case option4 = "Option 4"
        case option5 = "Option 5"
        case option6 = "Option 6"
        case option7 = "Option 7"
    }
    static let allOptions : [Option] = [.option1, .option2, .option3, .option4, .option5, .option6, .option7]
    static var selectedOptions : [Option] = [.option2, .option7]
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var optionsTableView: UITableView!

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Options.allOptions.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = Options.allOptions[indexPath.row].rawValue
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        Options.selectedOptions.append(Options.allOptions[indexPath.row])
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        let option = Options.allOptions[indexPath.row]
        for o in Options.selectedOptions {
            if option == o {
                let i = Options.selectedOptions.index(of: o)!
                Options.selectedOptions.remove(at: i)
            }
        }
    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let option = Options.allOptions[indexPath.row]
        if Options.selectedOptions.contains(option) {
            cell.setSelected(true, animated: false)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.optionsTableView.allowsMultipleSelection = true
        self.optionsTableView.delegate = self
        self.optionsTableView.dataSource = self
    }

}

推荐答案

cell.setSelected(true, animation: false)实际上并未选择 tableview 中的单元格.单元格被选中后回调.相反,你必须打电话tableView.selectRow(at: indexPath, animation: false, scrollPosition: .none)

cell.setSelected(true, animated: false)is not actually selecting the cell in tableview. It is callback after the cell is selected. Instead, you have to call tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)

你的 willDisplay 函数应该是:

Your willDisplay function should be:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    let option = Options.allOptions[indexPath.row]
    if Options.selectedOptions.contains(option) {
        tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
    }
}

这篇关于当实现 willDisplay 功能时,UITableViewCell 不会取消选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆