试图开发一个检查单应用程序,并且一直试图保存选中标记的状态。当我离开tableView并返回所有保存的复选标记时,它们将被删除。我已经导入UKIT,然后定义了这个类。如何将单选标记附件保存在单元格中
这里是我的代码:
var PreDefinedTasks = ["1", "2", "3", "4"]
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
if cell.accessoryType == .checkmark{
cell.accessoryType = .none
}
else{
cell.accessoryType = .checkmark
}
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return PreDefinedTasks.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "List1", for: indexPath)
cell.textLabel?.text = PreDefinedTasks[indexPath.row]
return cell
}
我已经看着NSCoder作为一个解决方案,但不能似乎得到它才能正常工作。任何帮助表示赞赏!
您可以为选中和未选中的项目保留一个列表数组,并根据它设置cell.assessoryType。 –