如何将复选标记状态保存在核心数据中?

发布于 2024-08-20 04:10:54 字数 554 浏览 11 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是一片海 2024-08-27 04:10:54

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

- (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 的委托,因此我对查询中的托管对象所做的更改 ^^^ 将导致以下两个方法跑步。

- (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”属性以查看单元格是否应该具有是否打勾。

为了避免您感到困惑,我最初使用通用的 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.

- (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"];
    }
}

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;
}

Here's how everything ties together

  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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文