Swift 如何在核心数据应用程序的 UITableView 中保存复选标记状态
How to save checkmark state in a UITableView for Core Data App in Swift
我已经使用 Core Data.What 实现了一个列表应用程序 Data.What 我想做的是添加复选标记,以便用户可以单击一个单元格,然后会出现一个勾号以表明它已完成。
我不知道如何保存这些复选标记,所以当应用程序关闭、打开或重新启动时,复选标记仍会存在于特定的单元格上。我应该用 CoreData 还是 NSUserDefaults 来实现它?我将如何使用这些实现它?
目前的代码如下:
cellForRowAtIndex :
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell
let item = Items[indexPath.row]
cell.textLabel?.item = task.valueForKey("item") as? String
return cell
}
DidSelectRowAtIndex:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedRow:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
if selectedRow.accessoryType == UITableViewCellAccessoryType.None {
selectedRow.accessoryType = UITableViewCellAccessoryType.Checkmark
selectedRow.tintColor = UIColor.greenColor()
} else {
selectedRow.accessoryType = UITableViewCellAccessoryType.None
}
}
任何帮助将不胜感激。
您需要在数据模型(核心数据对象)而不是单元格中跟踪完成状态。正如您所发现的那样,单元格会被重新使用,而且单元格不会持久存在,因此不会保存复选标记状态。
我会用这样的东西:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let taskObject=self.listItems[indexPath.row]
var taskStatus = taskObject.valueForKey("completed") as? Bool ?? false
taskObject.setValue(!taskStatus,forKey:"completed") // Toggle completed status
do {
try managedContext.save()
} catch let error as NSError {
print("Cannot save object: \(error), \(error.localizedDescription)")
}
tableView.deselectRowAtIndexPath(indexPath,animated:false)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
}
然后,在您的 cellForRowAtIndexPath
中,您可以适当地呈现该行:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("listCell") as! UITableViewCell
let task = listItems[indexPath.row]
let taskStatus = task.valueForKey("completed") as! Bool
cell.textLabel?.text = task.valueForKey("task") as? String
var accessoryType=UITableViewCellAccessoryType.None
var tintColor = UIColor.clearColor()
if (taskStatus) {
accessoryType=UITableViewCellAccessoryType.Checkmark
tintColor = UIColor.greenColor()
}
cell.accessoryType=accessoryType
cell.tintColor = tintColor
return cell
}
我已经使用 Core Data.What 实现了一个列表应用程序 Data.What 我想做的是添加复选标记,以便用户可以单击一个单元格,然后会出现一个勾号以表明它已完成。
我不知道如何保存这些复选标记,所以当应用程序关闭、打开或重新启动时,复选标记仍会存在于特定的单元格上。我应该用 CoreData 还是 NSUserDefaults 来实现它?我将如何使用这些实现它?
目前的代码如下:
cellForRowAtIndex :
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell
let item = Items[indexPath.row]
cell.textLabel?.item = task.valueForKey("item") as? String
return cell
}
DidSelectRowAtIndex:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedRow:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
if selectedRow.accessoryType == UITableViewCellAccessoryType.None {
selectedRow.accessoryType = UITableViewCellAccessoryType.Checkmark
selectedRow.tintColor = UIColor.greenColor()
} else {
selectedRow.accessoryType = UITableViewCellAccessoryType.None
}
}
任何帮助将不胜感激。
您需要在数据模型(核心数据对象)而不是单元格中跟踪完成状态。正如您所发现的那样,单元格会被重新使用,而且单元格不会持久存在,因此不会保存复选标记状态。
我会用这样的东西:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let taskObject=self.listItems[indexPath.row]
var taskStatus = taskObject.valueForKey("completed") as? Bool ?? false
taskObject.setValue(!taskStatus,forKey:"completed") // Toggle completed status
do {
try managedContext.save()
} catch let error as NSError {
print("Cannot save object: \(error), \(error.localizedDescription)")
}
tableView.deselectRowAtIndexPath(indexPath,animated:false)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
}
然后,在您的 cellForRowAtIndexPath
中,您可以适当地呈现该行:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("listCell") as! UITableViewCell
let task = listItems[indexPath.row]
let taskStatus = task.valueForKey("completed") as! Bool
cell.textLabel?.text = task.valueForKey("task") as? String
var accessoryType=UITableViewCellAccessoryType.None
var tintColor = UIColor.clearColor()
if (taskStatus) {
accessoryType=UITableViewCellAccessoryType.Checkmark
tintColor = UIColor.greenColor()
}
cell.accessoryType=accessoryType
cell.tintColor = tintColor
return cell
}