【问题标题】:UITableViewCell not deselecting when willDisplay function is implemented实现willDisplay功能时UITableViewCell没有取消选择
【发布时间】:2018-12-03 04:11:22
【问题描述】:

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

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
    }

}

【问题讨论】:

  • 创建一个具有 name 和 isSelected 属性的模型选项。然后根据选择操作 isSelected 属性。使用 allOption: [Option] 作为表的模型。您不需要单独的模型 allOption 和 selectedOption。
  • 我很抱歉,但我不关注。 “模特”是什么意思?
  • 我的意思是使用 struct Option { name: String, isSelected: Bool} 然后在用户选择或取消选择 didSelectRowAt 的选项时切换 isSelected 然后重新加载。您不需要 willDisplay 或 didDeselectRowAt。

标签: ios swift uitableview


【解决方案1】:

cell.setSelected(true, animated: false)实际上并没有选择表格视图中的单元格。选择单元格后回调。相反,您必须致电 tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)

你的 willDisplay 函数应该是:

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

【讨论】:

  • 请补充说明
  • 似乎 setSelected 函数禁用了委托函数 didSelect 和 didDeselect。
  • @mightknow 如果它是你的解决方案,你能接受我的回答吗?谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多