我非常接近解决方案,但我无法弄清楚剩下的,所以我现在正在寻求帮助...... 我使用核心数据,所以我想展示一下,内部是什么。 到目前为止,这是我的代码:
at .m
@property NSMutableArray *users, *sharedWishes;
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *delegate=[UIApplication sharedApplication].delegate;
self.context=delegate.managedObjectContext;
self.users=delegate.collectUserNames; //collect every users from database
self.sharedWishes=[delegate collectSharedWishes]; //collect every wish from DB - each user has different number of records
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// works great so far
return self.users.count;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
User *user=[self.users objectAtIndex:section]; //shows the username for each section
if(self.users.count<1)
return @"no friends";
else
return user.userName; //this is just fine, too
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SharedWishes *wish=[self.sharedWishes objectAtIndex:indexPath.row]; //this is where the problem is...the tableview shows the records until the other user records starts
static NSString *identifier=@"Cell";
Cell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
...
}
那么,对于 cellForRowAtIndexPath:的任何建议,我应该添加什么? 每个部分都应该有一个当前用户名的标题(有),每个部分都应包含该特定人的记录(失败)。相反,当新部分开始(使用新用户)时,现有记录开始重复。我应该如何避免它? 提前谢谢!
编辑:将这几行添加到cellForRowAtIndexPath:
User * user = [self.users objectAtIndex:indexPath.section];
AppDelegate * delegate = [UIApplication sharedApplication] .delegate;
self.currentUserWishes = [delegate collectCurrentUserSharedWishes:user];
现在有效!