我复制从苹果的代码文件的NSFetchedResultsController
。它说,sectionNameKeyPath
如果我想为显示结果的UITableView划分我的结果,则应提供。
我旨在根据模型对象名称开头的字符来区分结果。即按名称排序。
这段代码完全可以做到这一点(按名称排序),但是每一行都显示在自己的部分中,而不是对行组进行分区。(节数基本上等于行数)。
我相信我在某个地方犯了一个愚蠢的错误,但是现在我花了两个半小时,看不到任何东西。我只是要粘贴要在其中显示数据的UITableView的委托/数据源方法。
#pragma mark NSFetchedResultsController
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [Place MR_requestAllSortedBy:@"name" ascending:YES];
[fetchRequest setFetchLimit:0];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[NSManagedObjectContext MR_contextForCurrentThread] sectionNameKeyPath:@"name" cacheName:@"places"];
_fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
#pragma mark UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:listTableViewCellIdentifier];
Place *place = [_fetchedResultsController objectAtIndexPath:indexPath];
FAKIcon *categoryIcon = [FFPlaceHandler getIconForPlaceWithCategoryID:place.main_category andSize:CELL_ICON_SIZE];
[categoryIcon addAttribute:NSForegroundColorAttributeName value:FF_WHITE_COLOR];
FAKIcon *circleIcon = [FAKFontAwesome circleIconWithSize:CELL_ICON_CIRCLE_SIZE];
[circleIcon addAttribute:NSForegroundColorAttributeName value:[FFPlaceHandler getColorForPlaceWithCategoryID:place.main_category]];
cell.image.image = [UIImage imageWithStackedIcons:@[circleIcon, categoryIcon] imageSize:CGSizeMake(CELL_ICON_CIRCLE_SIZE, CELL_ICON_CIRCLE_SIZE)];
cell.title.text = place.name;
cell.addressLabel.text = place.place_contact_info.address;
[cell setDistanceWithDistance:[place.distance_from_user floatValue]];
cell.indexPath = indexPath;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark UITableViewDatasource
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if ([[_fetchedResultsController sections] count] > 1) {
id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo name];
} else
return nil;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [_fetchedResultsController sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [_fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40.0;
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
if ([[_fetchedResultsController sections] count] > 1) {
id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects] ;
} else
return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[_fetchedResultsController sections] count];
}
为什么UITableViewCell
分配给的每个部分都没有按字母顺序排列?
编辑:
通过在核心数据中的放置表中添加字段来解决该问题。该字段保存每个记录的第一个字符。在此之后,我更改了sectionKeyPath
在此方法中看到的内容:
#pragma mark NSFetchedResultsController
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [Place MR_requestAllSortedBy:@"name" ascending:YES];
[fetchRequest setFetchLimit:0];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[NSManagedObjectContext MR_contextForCurrentThread] sectionNameKeyPath:@"name_first_character" cacheName:@"places"];
_fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
此外i取代了> 1
与> 0
所建议MOBY!我现在有一些不错的部分:)
按keypath进行的分区name
将为每个具有不同名称值的模型创建一个新的分区。因此,如果您有20个模型并且它们都具有不同的name
值,则将有20个部分。如果您有20个模型,其中10个模型具有name
A,其中10个模型具有name
B,则将有2个部分。
另外,在您的
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
您正在检查
if ([[_fetchedResultsController sections] count] > 1)
相反,我认为您应该在检查count > 0
而不是1。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句