titleForHeaderInSection is not updating Section Header information correctly
I have two tableviews where one navigates to the next. Both use more or less the same code and use the managedObjectContext and FetchedResultsController design pattern to provide the relevant data for both tables.
My problem happens when I navigate from the first to the second tableview and when the datasource has only one Section Header / Row to display in the tableview. I'm finding the datasource does not update Section Header information. It will however update this information if there is more than one row or section data passed the second tableview.
This issue is only really noticeable when attempting to navigate the same criteria of data from a different row of the first tableview a second time. Somehow it has not released the information from the first attempt and instead has used this information as a placeholder. I can confirm memory management methods have been done and that the corresponding Row below the Section Header is using the correct data so my managedObjectModel is being passed correctly.
Has anyone else come across this problem?
Update
This is the code used in the first tableview.
- (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:NSLocali开发者_JS百科zedString(@"%@ - %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];
}
}
}
This is the code used in the second 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];
}
}
I solved this issue by supplying a nil value to cacheName in the initWithFetchedRequest method of my FetchedResultsController. Another recommendation was to delete the cache before instantiating the NSFetchedResultsController using the following method:
+ (void)deleteCacheWithName:(NSString *)name;
All credit for this answer must go to 'unforgiven' for an answer posted here.
精彩评论