試圖開發一個檢查單應用程序,並且一直試圖保存選中標記的狀態。當我離開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。 –