如何将复选标记状态保存在核心数据中?
我有一个列表应用程序,用户可以点击+按钮并输入他们想要出现在列表中的项目,然后点击保存。该表与核心数据一起保存。唯一的问题是当单元格被粘贴时我想要显示一个复选标记。我启用了多重选择,
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
thisCell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
我希望在用户退出后复选标记保留在单元格中。我在我的实体中创建了一个名为“检查”的属性,并为其指定了布尔类型,但我不知道如何使其在点击一行时出现检查并保留。任何帮助将不胜感激。谢谢
I have a list app where users hit the + button and enter in an item that they want to appear in the list and hit save. The table is saved with core data. The only problem is when the cell is taped I want a checkmark to be displayed. Ive enabled multiple selection with
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
thisCell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
I would like the checkmarks to be persisted in the cell after the user exits. I have created an attribute in my entity called "checks" and gave it the type of boolean but I dont know how to make it where if you hit a row then a check appears and is persisted. Any help would be greatly appreciated. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我就是这样做的。值得注意的一点是:CoreData 不存储布尔值,因此任何标记为“boolean”的属性实际上都是
NSNumber
类型。在处理 CoreData 和布尔值时,您必须记住来回转换。我将
UITableViewController
设置为NSFetchedResultsController
的委托,因此我对查询中的托管对象所做的更改 ^^^ 将导致以下两个方法跑步。以下是所有内容如何联系在一起的
controllerDidChangeContent
方法。controllerDidChangeContent
方法只是重新加载表视图中的所有数据为了避免您感到困惑,我最初使用通用的
NSMangaggedObject
来存储行状态,这就是为什么我发布的第一个方法是[selectedObject valueForKey:@"isDone"]
。后来我切换到名为JKItem
的子类托管对象,这就是为什么第二组方法能够使用item.isDone
而不会生成编译器警告。This is how I do it. One notable point: CoreData does not store booleans, so any property labeled "boolean" is actually of type
NSNumber
. You've got to remember to convert back and forth when dealing with CoreData and boolean values.I have my
UITableViewController
set as the the delegate for theNSFetchedResultsController
, so the changes I made to the managed objects in the query ^^^ will cause the following two methods to be run.Here's how everything ties together
controllerDidChangeContent
method on its delegate.controllerDidChangeContent
method just reloads all the data in the table viewAnd just so you don't get confused, I initially used a generic
NSMangagedObject
to store row state, which is why the first method I posted says,[selectedObject valueForKey:@"isDone"]
. Later I switched to a subclassed managed object namedJKItem
, which is why the second set of methods is able to useitem.isDone
without generating a compiler warning.