4

我正在使用 NSFetchedResultsController 填充 UITableViewController,结果创建填充节标题和节索引的节。我正在使用以下方法来填充部分索引:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [fetchedResultsController_ sectionIndexTitles];
}

现在我遇到了一个问题。当我将新元素添加到与 NSFetchedResultsController 关联的 NSManagedObjectContext 时,新元素将被保存并适当地显示为 UITableView 中的单元格......除了一件事。如果新元素创建了一个新的 SECTION,则新的部分索引不会显示在右侧边距中,除非我弹出 UINavigationController 的堆栈并重新加载 UITableViewController。

我已经符合 NSFetchedResultsControllerDelegate 的接口并手动调用

[self.tableView reloadSectionIndexTitles];

在这两个委托方法的末尾:

controller:didChangeSection... 
controller:didChangeObject...

虽然我可以调试并跟踪方法的执行并查看调用的重新加载调用,但 UITableView 的部分索引从不反映部分更改。

再次显示数据——新部分在 UITableView 中物理可见(或删除),但部分索引未更新。

我错过了什么吗?

4

5 回答 5

7

看起来这是我们都有的错误。请参阅http://iphonedevelopment.blogspot.com/2009/11/i-know-youre-tired-of-hearing-about.html了解在我看来是一个相当讨厌的太多代码行的解决方案。我和这个一起去了:

- (void)viewWillAppear:(BOOL)animated; {
  // This is a dumb hack required by this bug: http://iphonedevelopment.blogspot.com/2009/11/i-know-youre-tired-of-hearing-about.html
  [self.tableView reloadData];
}

它可能效率低下,但除非您拥有大量数据,否则它可能不会造成任何伤害。而且它只有 1 行代码。因此,当苹果修复他们的错误时,您可以轻松地将其删除。

于 2010-01-06T22:24:18.047 回答
1

问题已经2个月了,但我今天遇到了同样的问题。似乎-reloadSectionIndexTitles根本不起作用,所以我尝试了一些潜在的黑客攻击,以下哪些对我有用:

@implementation UITableView (JKAdditions)

- (UIView *)indexView {
    Class indexClass = NSClassFromString(@"UITableViewIndex");
    for(UIView *subview in self.subviews){
        if([subview isKindOfClass:indexClass]) return subview;
    }
    return nil;
}

- (void)reloadSectionIndexTitles {
    UIView *indexView = [self indexView];
    [indexView performSelector:@selector(setTitles:) withObject:[self.dataSource sectionIndexTitlesForTableView:self]];
    [indexView setNeedsDisplay];
}

@end

我真的不知道 Apple 是否会因为这次黑客攻击而拒绝你的应用程序,但这似乎是我唯一的选择。重新加载整个 tableView 根本不是我想要的,因为我必须处理各种动画问题。

我希望这可以帮助任何有同样问题的人!

于 2010-03-28T21:40:07.443 回答
1

要将接受的答案与 Alex Reynolds 的答案与延迟相结合,只需调用reloadData与动画持续时间相对应的延迟,即 0.4 或 0.3 秒。

就我而言,我将延迟的方法调用粘贴到控制器中:didChangeSection:atIndex:forChangeType:(它是一个核心数据应用程序)。

添加或删除部分时,结果是单元格的标准动画,然后是重新加载数据时更新的索引。

It's ugly and makes me cringe, but I am okay with the result. I also submitted a bug to Apple, #8589547.

于 2010-10-25T15:26:13.040 回答
0

试着把它放在末尾-controllerDidChangeContent:,之后的某个地方[self.tableView endUpdates]

于 2010-01-03T23:17:28.893 回答
0

我做的另一件事(对我有用,不能保证它对你有用)是在很短的延迟后执行选择器,例如:

[self performSelector:(@selector(refreshSectionIndex)) withObject:nil afterDelay:0.2];

// ...

- (void) refreshSectionIndex {
    [self.tableView reloadSectionIndexTitles];
}    

核心数据,NSFetchedResultsController尤其是看起来像地狱一样的错误,委托表视图更新与获取的数据不同步,导致应用程序崩溃。我真的希望 Apple 正在采取措施修复 4.0 SDK 中这些框架中的错误。这很令人沮丧。

于 2010-01-06T22:36:31.637 回答