0

I have a UITableView that adds information from a Core Data the following way:

  1. The "Category" for various names is added at the header
  2. The names that correspond to the Category should be loaded in cells beneath the Category

Right now I have the header loading the right name and the right number of sections and rows being added. However - the results from Core Data are being returned as an NSSet, so I can't add each name to the cells based on the indexPath.row.

Any suggestions? For reference:

cell.textLabel.text = [NSString stringWithFormat:@"%@",[[[dataToUse objectAtIndex:indexPath.section]valueForKey:@"heldBy"]valueForKey:@"name"]];

Returns the appropriate set of names to each cell of the appropriate section, but it returns the ENTIRE set to each cell. I just want each name to be added based on which row it's a part of. I can solve this by converting the NSSet to an Array, but since there are multiple sets being created (because there are multiple categories) I don't see how I can do this.

EDIT: I fixed my problem by doing the following, but I'm still interested to know what the best thing to do would have been.

NSSet *daset = [[dataToUse objectAtIndex:indexPath.section]valueForKey:@"heldBy"];
NSMutableArray *addToLabel = [[NSMutableArray alloc]init];
int i = 0;
for(NSSet *contact in daset) {
    [addToLabel insertObject:[contact valueForKey:@"name"] atIndex:i];
    NSLog(@"%@",addToLabel);
    i++;
}
cell.textLabel.text = [NSString stringWithFormat:@"%@",[addToLabel objectAtIndex:indexPath.row]];
4

3 回答 3

1

使用NSFetchedResultsController

它旨在完全满足您的需求。对于像您这样的直截了当的情况,您只需要根据您的模型关系将数据组织成部分,它会通过自动管理获取、编辑、缓存等来减轻您的负担。您可以在这里找到一个不错的教程和当然这里是官方文档

于 2013-06-18T07:24:59.050 回答
0

要从 NSSet->NSArray 转换,您需要使用 NSSortDescriptior 对集合进行排序。就像是:

NSSortDescriptor *sort=[[NSSortDescriptor alloc] initWithKey:@"nm" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];

  NSArray *arr = [[myset allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
于 2013-06-17T04:16:46.207 回答
0

NSSortDescriptor *sort=[[NSSortDescriptor alloc] initWithKey:@"name" 升序:YES 选择器:@selector(localizedCaseInsensitiveCompare:)];

NSArray *arr = [[myset allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

"nsset 对象" *nssetObject =[arr objectAtIndex:indexPath.row];

cell.textLabel.text = [NSString stringWithFormat:@"%@",nssetObject.name];

于 2014-05-06T09:57:19.700 回答