使用NSSet关系填充cellForRow

我查看了许多帖子,仍然不知道如何解决这个问题。希望有人能提供帮助。 它一直有效,直到命中最后一个数据源方法cellForRow。 那时我可以为每个部分获得正确的NSSet,但是无序。关系属性的intropection如何适用于行? 在cellForRow中使用字符串文字,我确实在每个部分中获得了正确的行数,但显然没有与托管对象的连接。 如何从NSSet关系中填充行?所有见解都很受欢 分类和LT;< --- >>人 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;
}
    
已邀请:
您的问题是您正在获取
Category
对象,但您正在尝试使用
Person
对象设置行。
Person
对象是无序的,因为它们不是获取的对象。它们与tableview的逻辑结构无关。事实上,它们不能,因为它们与
Category
有很多关系,因此同一个
Person
对象可以在同一个表中多次出现。 最好的解决方案是将其分解为两个分层表。一个显示分类列表,第二个显示在第一个表视图中选择的
Category
对象的
people
关系中的
Person
个对象。 您可以通过尝试以下方式尝试使其与当前设计一起使用:
- (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;
如果您的数据是静态的,这将有效。如果在表格显示时它发生变化,您将遇到麻烦。它会产生一些开销,因为每次填充行时都必须获取和排序
sectionCategory
对象的所有
Person
对象。 我强烈建议使用两个tableview解决方案。这是分层数据的首选解决方案。     

要回复问题请先登录注册