使用 NSSet 关系来填充 cellForRow
我浏览了很多帖子,但仍然不知道如何解决这个问题。希望有人能帮忙。
它一直有效,直到遇到最后一个数据源方法 cellForRow。 那时我可以获得每个部分的正确 NSSet,但无序。对行的关系属性的内省如何工作?
在 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;
}
I've looked through many posts and still not sure how to resolve this. Hope someone can help.
It works until hitting the last data source method, cellForRow.
At that point I can get the correct NSSet for each section, but unordered. How does intropection into the relationship properties work for the rows?
using a string literal in the cellForRow I do get the correct number of rows in each section, but obviously no connection to the managed objects that would be there.
How can I populate the rows from the NSSet relationship? All insight appreciated
Category<<--->>Person
cName ---------- pName
relationships
people ---------- categories
- (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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是您正在获取
Category
对象,但您尝试使用Person
对象设置行。Person
对象是无序的,因为它们不是获取的对象。它们与表视图的逻辑结构没有关系。事实上,它们不能,因为它们与Category
具有一对多关系,因此同一个Person
对象可以在同一个表中多次出现。最好的解决方案是将其分解为两个层次结构表。第一个显示类别列表,第二个显示第一个表视图中选择的
Category
对象的people
关系中的Person
对象。您可以尝试通过尝试以下操作来使其与当前设计一起工作:
如果您的数据是静态的,这将起作用。如果在表格显示时它发生变化,您就会遇到麻烦。它会产生一些开销,因为每次填充一行时都必须获取
sectionCategory
对象的所有Person
对象并对其进行排序。我强烈推荐两个表视图解决方案。这是分层数据的首选解决方案。
Your problem is that you are fetching
Category
objects but you are trying to set your rows withPerson
objects. ThePerson
objects are unordered because they are not the fetched objects. They have no relationship to the logical structure of the tableview. In fact, they can't because they have a to-many relationship withCategory
such that the samePerson
object can show up in many times in the same table.The best solution is to decompose this into two hierarchal tables. One displays the Category list and the second displays the
Person
objects in thepeople
relationship of theCategory
object chosen in the first tableview.You can attempt to get it working with the current design by trying something like:
This will work if your data is static. If it changes while the table displays, you will have trouble. It will have a bit of overhead because you have to fetch and sort all the
Person
objects of thesectionCategory
object each time you populate a row.I strongly recommend a two tableview solution. That is the preferred solution for hierarchal data.