I have a UITableView
that loads entities from Core Data using a NSFetchedResultsController
. The fetch request is currently using a batch size to keep memory overhead under control as there are about 1000 rows being returned from the fetch. After hooking all this up, the table view was really fast.
现在,我的问题是,我想在表态中增加一组标题。 我已采用了必要的代表方法,但似乎在“条码”表:标题ForHeaderIn Section:
是否有更好的办法填充该节标题,或者,更糟的是,是否可把标题的加添到与实际桌子一样的用户卷上?
值得注意的一些事项:
- I am telling the fetch request to pre-fault
dateOfFlight
using thesetPropertiesToFetch:
method. - The fetch request has a batch limit of 25.
- The fetch request has no fetch limit.
- I do not need section index titles. For
sectionIndexTitlesForTableView:
, I am returningNil
; - There are about 1000 entities returned from the fetch and, because we re grouping by a localized date, there are about 700 sections.
对于其价值而言,本条准则一用在标题上:
/**
* We override this method from the base class because we need to format the date
* prior to it being displayed in the UITableViewSection.
* @author Jesse Bunch
**/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
Flight *cellEntity = (Flight *)[[sectionInfo objects] objectAtIndex:0];
if (cellEntity) {
return [cellEntity.dateOfFlight localizedLongDateString];
}
return L(@"Unknown Date");
}
-
Edit
我的确通过从款次名称而不是实体本身对日期进行格式化,实现了比较高的速度:
/**
* We override this method from the base class because we need to format the date
* prior to it being displayed in the UITableViewSection.
* @author Jesse Bunch
**/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
static NSDateFormatter *dateFormatter;
if(nil == dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss ZZ";
}
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSDate *sectionDate = [dateFormatter dateFromString:sectionInfo.name];
if (sectionDate) {
return [sectionDate localizedLongDateString];
}
return L(@"Unknown Date");
}