2017-07-28 32 views
1

试图开发一个检查单应用程序,并且一直试图保存选中标记的状态。当我离开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作为一个解决方案,但不能似乎得到它才能正常工作。任何帮助表示赞赏!

+0

您可以为选中和未选中的项目保留一个列表数组,并根据它设置cell.assessoryType。 –

回答

1

下面是我如何去做,如果你遵循整个解决方案,即使应用程序关闭,它也会保存。

使一个这样的数组断型布尔:var checkmarks = [Int : Bool]()

然后,在cellForRow功能,补充一点:

if checkmarks[indexPath.row] != nil { 
    cell.accessoryType = checkmarks[indexPath.row] ? .checkmark : .none 
} else { 
    checkmarks[indexPath.row] = false 
    cell.accessoryType = .none 
} 

而在didSelectRow功能,补充一点:

if let cell = tableView.cellForRow(at: indexPath as IndexPath) { 
    if cell.accessoryType == .checkmark{ 
     cell.accessoryType = .none 
     checkmarks[indexPath.row] = false 
    } 
    else{ 
     cell.accessoryType = .checkmark 
     checkmarks[indexPath.row] = true 
    } 
} 

如果您希望在应用程序关闭时保存它,则必须通过以下操作将检查标记数组保存在UserDefaults中:

在didSelectRow功能,在一切后,底部补充一点:

UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: checkmarks), forKey: "checkmarks") 
UserDefaults.standard.synchronize() 

然后又在viewDidLoad中,补充一点:

if let checks = UserDefaults.standard.value(forKey: "checkmarks") as? NSData { 
    checkmarks = NSKeyedUnarchiver.unarchiveObject(with: checks as Data) as! [Int : Bool] 
} 

让我知道如果这个答案帮助,如果您有任何疑问。

编辑: 所以我完全忘了,[Int:Bool]不是一个NSDictionary,它只是一个字典。 UserDefaults不能存储Dictionary对象,只能使用NSDictionary,这就是为什么你必须将它更改为NSData,因此它能够保存[Int:Bool]。希望它这次有效:)

+0

谢谢,这的确帮助我更深入地理解数据持久性。即使应用程序关闭,我如何更改此数据以保留数据?再次,谢谢你的回复:)编辑:我无法看到你的答案的其余部分抱歉。我讨厌iMacs :( –

+0

我很奇怪的措辞,但我的答案的最后一部分显示了当应用程序关闭时如何保存 – Timmy

+0

是啊对不起,你的措辞很好,非常简洁,谢谢你的帮助 –

0

您可以根据tableView行上选中的复选标记将索引路径保存到数组中。

+0

你能建议我会怎么做吗?感谢您的回复:) –

+0

因此,在您的didSelectRow委托方法中,您可以获得已选择哪些行的索引路径。你可以存储在数组中,当你重新加载你的表时,如果匹配,检查indexpath设置单元格上的复选标记。 –

0

根据你的实现,UITableViewCell被重用与标识符为“List1”。所以如果你想重新使用这个单元格,那么你必须通过将状态存储在预定义的任务中来更新正确的accessoryType。

0

由于单元格已卸载并稍后重用,因此您需要在其他位置存储复选标记的状态。在这个例子中名为preDefinedTaskCheckmarkState的数组中。加载单元格时,您还需要设置复选标记状态。

var PreDefinedTasks = ["1", "2", "3", "4"] 
var preDefinedTaskCheckmarkState = [Bool](repeating: false, count: 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) { 

     preDefinedTaskCheckmarkState[indexPath.row] = !(cell.accessoryType == .checkmark) 

     cell.accessoryType = preDefinedTaskCheckmarkState[indexPath.row] ? .checkmark : .none 
    } 
} 

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] 
    cell.accessoryType = preDefinedTaskCheckmarkState[indexPath.row] ? .checkmark : .none 

    return cell 
} 
+0

这是一种我没有考虑的方法。谢谢你的回复是有用的:) –

+0

没问题,希望它能帮你一下:) – Christoph

相关问题