titleForHeaderInSection 未正确更新节标题信息

Posted

技术标签:

【中文标题】titleForHeaderInSection 未正确更新节标题信息【英文标题】:titleForHeaderInSection is not updating Section Header information correctly 【发布时间】:2010-05-22 21:50:08 【问题描述】:

我有两个表视图,其中一个导航到下一个。两者都使用或多或少相同的代码,并使用 managedObjectContext 和 FetchedResultsController 设计模式为两个表提供相关数据。

当我从第一个 tableview 导航到第二个 tableview 并且数据源只有一个 Section Header / Row 显示在 tableview 中时,我的问题发生了。我发现数据源没有更新部分标题信息。但是,如果有超过一行或部分数据通过第二个表视图,它将更新此信息。

只有在第二次尝试从第一个 tableview 的不同行导航相同的数据标准时,此问题才会真正引人注目。不知何故,它没有从第一次尝试中发布信息,而是将此信息用作占位符。我可以确认内存管理方法已经完成,并且 Section Header 下面的相应 Row 正在使用正确的数据,因此我的 managedObjectModel 正在正确传递。

还有其他人遇到过这个问题吗?

更新

这是第一个表格视图中使用的代码。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  

id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];

if ([fetchSectioningControl selectedSegmentIndex] == 2)        
    return [NSString stringWithFormat:NSLocalizedString(@"%@ - %d events", @"%@ - %d events"), [sectionInfo name], [sectionInfo numberOfObjects]];

 else if ([fetchSectioningControl selectedSegmentIndex] == 1) 
    return [NSString stringWithFormat:NSLocalizedString(@"%@ - %d events", @"%@ - %d events"), [sectionInfo name], [sectionInfo numberOfObjects]];

 else 
    NSString *dates = [self.dateFormatter stringFromDate:date];
    NSString *compareDate = [sectionInfo name];

    if ([dates isEqualToString:compareDate]) 
        return [NSString stringWithFormat:@"Today"];

    else 
        return [sectionInfo name];
    


这是第二个tableview中使用的代码

- (NSString *)tableView:(UITableView *)table titleForHeaderInSection:(NSInteger)section  

id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
NSString *dates = [self.dateFormatter stringFromDate:date];
NSString *compareDate = [sectionInfo name];
if ([dates isEqualToString:compareDate]) 
    return [NSString stringWithFormat:@"Today"];
else 
    return [sectionInfo name];


【问题讨论】:

如果您包含一些代码,将更容易查明问题。您能否在 TableViewDataSources 中添加 tableView:titleForHeaderInSection: 方法的代码?如果两个表的数据源之间的方法完全不同,请同时包含这两个实现。 【参考方案1】:

我通过在我的 FetchedResultsController 的 initWithFetchedRequest 方法中为 cacheName 提供一个 nil 值解决了这个问题。另一个建议是在使用以下方法实例化 NSFetchedResultsController 之前删除缓存:

+ (void)deleteCacheWithName:(NSString *)name; 

对于发布在here 的答案,此答案的所有功劳必须归于“不可原谅”。

【讨论】:

以上是关于titleForHeaderInSection 未正确更新节标题信息的主要内容,如果未能解决你的问题,请参考以下文章

titleForHeaderInSection 与 UISearch 字段重叠

如何覆盖 tableView:titleForHeaderInSection: 以调整静态 UITableViews 的节标题?

UITableView titleForHeaderInSection 显示全部大写

iPhone tableview:titleForHeaderInSection 派生自数组

NSFetchedResultsController titleForHeaderInSection 带有格式化的 NSDate

如何填充 UITableView 部分标题?