Ios UITableViewCell在实现willDisplay功能时未取消选择
我有一个Ios UITableViewCell在实现willDisplay功能时未取消选择,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个UITableView,它显示了几个可用选项供用户选择。我想让表始终反映所选的选项,这些选项存储在一个数组中,该数组是独立于视图控制器的类的一部分。我试图使用tableView(\utableview:UITableView,willDisplay cell:UITableViewCell,forRowAt indexPath:indexPath)方法加载表时显示所选选项。我遇到的问题是,当我实现这个方法时,当表加载时数组中的任何选项在按下时都不会取消选择。代码如下: class Opt
UITableView
,它显示了几个可用选项供用户选择。我想让表始终反映所选的选项,这些选项存储在一个数组中,该数组是独立于视图控制器的类的一部分。我试图使用tableView(\utableview: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
}
}
cell.setSelected(true,动画:false)
实际上并没有在tableview中选择单元格。选中单元格后,它将被回调。相反,你必须打电话
tableView.selectRow(at:indepath,animated:false,scrollPosition:none)
您的显示功能应为:
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)
}
}
创建具有名称和选定属性的模型选项。然后在选择的基础上操作isSelected属性。使用allOption:[选项]作为表的模型。您不需要单独的型号allOption和selectedOption。对不起,我不懂。“model”是什么意思?我的意思是使用结构选项{name:String,isSelected:Bool},然后在用户选择或取消选择didSelectRowAt的选项时切换isSelected,然后重新加载。您不需要willDisplay或DidDeceloseRowat。请添加更多解释似乎setSelected函数已禁用,无法委托函数didSelect和DidDecelose。@可能知道这是您的解决方案,您能接受我的答案吗?非常感谢。