gpt4 book ai didi

ios - 使用 NSSet 关系填充 cellForRow

转载 作者:行者123 更新时间:2023-11-28 17:46:38 24 4
gpt4 key购买 nike

我看了很多帖子,仍然不确定如何解决这个问题。希望有人能帮忙。

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

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

如何从 NSSet 关系中填充行?感谢所有见解

类别<<--->>人物
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 对象可以在同一个表中多次出现。

最好的解决方案是将其分解为两个分层表。一个显示类别列表,第二个显示第一个 TableView 中选择的 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 的解决方案。这是分层数据的首选解决方案。

关于ios - 使用 NSSet 关系填充 cellForRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5602526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com