NSFetchedResultsController在UITableViewCell中具有明显的结果

时间:2017-01-10 20:51:35

标签: ios core-data

如果我理解正确,你必须使用setResultType:NSDictionaryResultType来允许setReturnsDistinctResults工作。

当我不使用setResultType:NSDictionaryResultType时,我没有得到预期的明显结果。

当我使用它时,我得到:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSKnownKeysDictionary1 objectID]: unrecognized selector sent to instance

我希望不同的结果填充UITableViewCell:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {





LocationInfo *info  = [_fetchedResultsController objectAtIndexPath:indexPath] ;
cell.textLabel.text =  info.fieldname;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@, %@",
                             info.date, info.acres, info.comments];

}

我正在使用一个简单的NSFetchedResultsController,其setResultType设置为NSDictionaryResultType:

NSManagedObjectContext *moc = [appDelegate managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Contacts" inManagedObjectContext:moc];
[fetchRequest setEntity:entity];


fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"fieldname"]];



NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                          initWithKey:@"fieldname" ascending:NO];

[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];



 [fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:@"fieldname"]];
[fetchRequest setReturnsDistinctResults:YES];




NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:moc sectionNameKeyPath:nil
                                               cacheName:@"Root"];

_fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = nil;

return _fetchedResultsController;

我是否不正确地为configureCell提供服务?这样做的正确方法是什么?

0 个答案:

没有答案