如何将复选标记状态保存在核心数据中? [英] How can a checkmark state be saved in core data?

查看:67
本文介绍了如何将复选标记状态保存在核心数据中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表应用程序,用户点击+按钮,并输入一个项目,他们想要出现在列表中,并点击保存。该表与核心数据一起保存。唯一的问题是当单元格胶带我想要显示一个复选标记。 Ive启用多个选择

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

推荐答案

这是我怎么做的。一个值得注意的一点是:CoreData不存储布尔值,因此任何标记为boolean的属性实际上是 NSNumber 类型。你必须记住在处理CoreData和布尔值时来回转换。

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.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSManagedObject *selectedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];


    if ([[selectedObject valueForKey:@"isDone"] boolValue]) {
        [selectedObject setValue:[NSNumber numberWithBool:NO] forKey:@"isDone"];
    } else {
        [selectedObject setValue:[NSNumber numberWithBool:YES] forKey:@"isDone"];
    }
}






我有我的 UITableViewController 设置为 NSFetchedResultsController 的委托,所以我对托管对象查询^^^将导致以下两种方法运行。


I have my UITableViewController set as the the delegate for the NSFetchedResultsController, so the changes I made to the managed objects in the query ^^^ will cause the following two methods to be run.

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *defaultCellIdentifier = @"Item";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:defaultCellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:defaultCellIdentifier] autorelease];
    }

    NSManagedObject *item = [[self fetchedResultsController] objectAtIndexPath:indexPath];
    cell.textLabel.text = [item valueForKey:@"name"];

    if ([[item valueForKey:@"checks"] boolValue]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

以下是一切联系在一起


  1. 用户点击一行

  2. tableView:didSelectRow ...方法更改了适当托管对象的isDone属性。
  3. 获取的结果控制器通知受管对象已更改并调用其代理上的 controllerDidChangeContent 方法。

  4. 我的 controllerDidChangeContent 方法只是重新加载表视图中的所有数据

  5. 当tableView重新加载时,我的tableView:cellForRow ...方法会检查受管理项目的isDone属性,以查看单元格是否应该有复选标记。

  1. User clicks on a row
  2. tableView:didSelectRow... method changes the "isDone" property of the appropriate managed object.
  3. the fetched results controller notices that a managed object has changed and calls the controllerDidChangeContent method on its delegate.
  4. My controllerDidChangeContent method just reloads all the data in the table view
  5. When the tableView is reloaded, my tableView:cellForRow... method checks the "isDone" property of the managed item to see if the cell should have a checkmark or not.

不要困惑,我最初使用一个通用的 NSMangagedObject 来存储行状态,这就是为什么我发布的第一个方法, [selectedObject valueForKey: @isDone] 。后来我切换到一个名为 JKItem 的子类管理对象,这就是为什么第二组方法能够使用 item.isDone ,而不生成编译器警告。

And 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 named JKItem, which is why the second set of methods is able to use item.isDone without generating a compiler warning.

这篇关于如何将复选标记状态保存在核心数据中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆