多个 NSFetchedResultsController - didChangeObject

发布于 12-11 19:09 字数 1157 浏览 2 评论 0原文

我有一个 UITableView,它使用 2 个 NSFetchedResultsControllers。每个 NSFetchedResultsController 只有一个部分。但是,该表有 4 个部分。我使用 NSFetchedResultsControllers 之一的结果填充表的第四部分。到目前为止一切正常。但是,如果用户删除第一部分中的第一个单元格,则 NSFetchedResultsControllers 会发生更改。表最后一部分中的行可能会被删除。调用此方法时:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
   atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;

switch(type) {
    case NSFetchedResultsChangeInsert:
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:
        NSLog(@"section: %d, row: %d", [newIndexPath section], [newIndexPath row]);

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;
...
}

该部分始终为 0,因为它是 NSFetchedResultsControllers 的部分。因此,该部分与表视图上的正确部分不匹配。

有解决方法吗?我基本上想将 NSFetchedResultsController 的部分更改为 3 而不是 0。

I have a UITableView which uses 2 NSFetchedResultsControllers. Each NSFetchedResultsController has only one section. However, the table has 4 sections. I populate the 4th section of the table with the results of one of the NSFetchedResultsControllers. Everything works fine so far. But if the user deletes the first cell on the first section, the NSFetchedResultsControllers are changed. The rows in the last section of the table might be deleted. When this method is called:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
   atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;

switch(type) {
    case NSFetchedResultsChangeInsert:
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:
        NSLog(@"section: %d, row: %d", [newIndexPath section], [newIndexPath row]);

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;
...
}

The section is always 0 because it's the section of the NSFetchedResultsControllers. Thus, the section doesn't match the correct one on the table view.

Is there a workaround? I would basically like to change the section of a NSFetchedResultsController to 3 instead of 0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

顾北清歌寒2024-12-18 19:09:03

我找到了一个解决方法,但如果有一个更漂亮的解决方案那就太好了。

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
   atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
if (newIndexPath != nil && controller == self.fetchedXController) {
    newIndexPath = [NSIndexPath indexPathForRow:[newIndexPath row] inSection:3];
    if ([tableView cellForRowAtIndexPath:newIndexPath] == nil) {
        type = NSFetchedResultsChangeInsert;
    }
}
if (indexPath != nil && controller == self.fetchedDomainsController) {
    indexPath = [NSIndexPath indexPathForRow:[indexPath row] inSection:3];
}

switch(type) {
    case NSFetchedResultsChangeInsert:
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeUpdate:
        [self configureCell:[tableView cellForRowAtIndexPath:newIndexPath] atIndexPath:newIndexPath];
        break;
...

I found a workaround, but it would be nice to have a prettier solution.

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
   atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
if (newIndexPath != nil && controller == self.fetchedXController) {
    newIndexPath = [NSIndexPath indexPathForRow:[newIndexPath row] inSection:3];
    if ([tableView cellForRowAtIndexPath:newIndexPath] == nil) {
        type = NSFetchedResultsChangeInsert;
    }
}
if (indexPath != nil && controller == self.fetchedDomainsController) {
    indexPath = [NSIndexPath indexPathForRow:[indexPath row] inSection:3];
}

switch(type) {
    case NSFetchedResultsChangeInsert:
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

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