1

我浏览了很多帖子,但仍然不确定如何解决这个问题。希望有人可以提供帮助。

它一直有效,直到到达最后一个数据源方法 cellForRow。那时我可以为每个部分获得正确的 NSSet,但无序。对关系属性的内省如何对行起作用?

在 cellForRow 中使用字符串文字我确实在每个部分中获得了正确的行数,但显然与那里的托管对象没有连接。

如何填充 NSSet 关系中的行?赞赏所有见解

类别<<--->>Person
cName ---------- pName

关系
人 ---------- 类别

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.fetchedResultsController fetchedObjects] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {
Category* cat = [[self.fetchedResultsController fetchedObjects] objectAtIndex:section];
return [[cat people] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
Category* cat = [[self.fetchedResultsController fetchedObjects] objectAtIndex:section];
NSNumber *rowCount = [NSNumber numberWithUnsignedInteger:[[cat people] count]]; 
return cat.cName;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
NSManagedObject *mo = [[fetchedResultsController fetchedObjects] objectAtIndex:index.row]];

// returns the correct set but unordered, possibly work with this to populate the rows?
//NSSet *theSet = [[NSSet alloc] initWithSet: [mo valueForKeyPath:@"people.pName"]];

// doesn't work
// cell.textLabel.text = [NSString stringWithFormat:@"%@",[mo valueForKeyPath:@"people.pName"]];

cell.textLabel.text = @"something";
return cell;
}
4

1 回答 1

0

您的问题是您正在获取Category对象,但您正在尝试使用Person对象设置行。对象是无序的Person,因为它们不是获取的对象。它们与 tableview 的逻辑结构没有关系。实际上,它们不能,因为它们具有一对多的关系,Category因此同一个Person对象可以在同一张表中多次出现。

最好的解决方案是将其分解为两个层次结构表。一个显示类别列表,第二个显示在第一个表视图中选择的Person对象的people关系中的Category对象。

您可以尝试通过以下方式尝试使其与当前设计一起使用:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    Category *sectionCategory=[[fetchedResultsController fetchedObjects] objectAtIndex:indexPath.section];
    NSSortDescriptor *sort=[NSSortDescriptor sortWithKey:@"pname" ascending:NO];
    NSArray *sortedPersons=[sectionCategory.people sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
    Person *rowPerson=[sortedPersons objectAtIndex:indexPath.row];
    cell.textLabel.text = rowPerson.pname;

如果您的数据是静态的,这将起作用。如果它在表格显示时发生变化,您将遇到麻烦。它会产生一些开销,因为每次填充一行时都必须获取Person对象的所有对象并对其进行排序。sectionCategory

我强烈推荐两个 tableview 解决方案。这是分层数据的首选解决方案。

于 2011-04-11T01:05:31.357 回答