通过NSFetchedResultsController在UITableView中显示重复的单元格? [英] Showing duplicate cells in UITableView via NSFetchedResultsController?

查看:50
本文介绍了通过NSFetchedResultsController在UITableView中显示重复的单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查询核心数据,以在一群人中找到最旧的"和最重的".对于较大的数据集,这通常是可行的(因为重复匹配的机会较少),但是对于较小的数据集,其中最老的人也可能是最重的人,我遇到了问题.

I am querying Core Data to find the "OLDEST" and "HEAVIEST" amongst a group of people. With a larger dataset this usually works (as the chance of a duplicate match is less), but with a small dataset where the OLDEST person may also be the HEAVIEST I am having problems.

[John, 77, 160]
[Pete, 56, 155]
[Jane, 19, 130]
[Fred, 27, 159]
[Jill, 32, 128]

由于我想在2个 UITableView 的UITableViewCell 中显示此信息,因此我首先要运行2个 NSFetchRequests (找到最旧的,第二个找到最重的)我是他们为每个托管对象获取 objectID 并将它们添加到我用来设置我的最终 NSFetchRequest 的过程中 NSFetchedResultsController .

As I want to display this information in 2 UITableViewCells of a UITableView I am doing this by first running 2 NSFetchRequests (one to find the OLDEST, the second to find the HEAVIEST) I am them taking the objectIDs for each managed object and adding them to a final NSFetchRequest that I am using to setup my NSFetchedResultsController.

// FETCH REQUEST - OLDEST, HEAVIEST
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"People"];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:descriptor]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", IDArray];
[fetchRequest setPredicate:predicate];

如果我打印最终的 NSFetchRequest 的描述",则确实包含2个指向managedObjects的指针.

If I "Print the Description" of the final NSFetchRequest it does indeed contain 2 pointers to managedObjects.

(i.e. [John, 77, 160] [John, 77, 160])

我的问题似乎是当我这样做

My problem seems to be that when I do

[[self fetchedResultsController] performFetch:nil];

UITableView 委托方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *sectionArray = [[self fetchedResultsController] sections];
    id <NSFetchedResultsSectionInfo> sectionInfo = [sectionArray objectAtIndex:0];
    NSUInteger numberOfRows = [sectionInfo numberOfObjects];
    NSLog(@"ROWS: %u", numberOfRows);
    return numberOfRows;
}

仅将 numberOfRows 显示为1,并且在我的 UITableView 中仅显示单个[John,77,160].

only shows the numberOfRows as 1 and only displays a single [John, 77, 160] in my UITableView.

推荐答案

获取请求不会返回重复的对象.它返回所有与谓词匹配的对象.所以

A fetch request does not return duplicate objects. It returns all objects that match the predicate. So

[NSPredicate predicateWithFormat: @"objectID = %@", oid]
[NSPredicate predicateWithFormat: @"(objectID = %@) OR (objectID = %@)", oid, oid]
[NSPredicate predicateWithFormat: @"objectID IN %@", @[oid, oid]]

返回具有给定对象ID(如果有)的所有一个对象.

return all one object with the given object ID (if there is one).

在您的情况下,您已经具有要显示的对象.因此,我建议将它们存储在数组中,并将其用作表视图数据源,而不要使用获取的结果控制器.

In your case, you have already the objects that you want to display. Therefore I would suggest to store them in an array and use that as table view data source, instead of using a fetched results controller.

这篇关于通过NSFetchedResultsController在UITableView中显示重复的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆