我的数组充当了ViewDidAppear
方法中填充的数据源。
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
PFQuery *userQuary = [PFUser query];
PFRelation *friendRelation = [self.currentUser relationForKey:@"friendRelation"];
PFQuery *acceptedQuary = [friendRelation query];
PFQuery *pendingFriends = [PFQuery queryWithClassName:@"FriendRequest"];
inQuery:aceptedFriends];
[pendingFriends whereKey:@"toUser" equalTo:self.currentUser.objectId];
[pendingFriends whereKey:@"status" equalTo:@"Pending"];
[userQuary whereKey:@"objectId" matchesKey:@"fromUser" inQuery:pendingFriends];
[userQuary findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error){
NSLog(@"ERROR: %@ %@", error, [error userInfo]);
}else{
self.friendList = objects;
[self.tableView reloadData];
}
}];
[acceptedQuary findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error){
NSLog(@"ERROR: %@ %@", error, [error userInfo]);
}else{
self.accepted = objects;
NSLog(@"%@", self.accepted);
[self.tableView reloadData];
}
}];
}
这是一个包含多个表格部分的应用程序,这里是发生崩溃的CellForRowAtIndexPath
方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
/*
PFUser *friend = [self.friendList objectAtIndex:indexPath.row];
cell.textLabel.text = friend.username;
cell.detailTextLabel.text= friend.email;
cell.imageView.image = [UIImage imageNamed:@"Treehouse.png"];
*/
PFUser *friend = nil;
if ([self.friendList count]> indexPath.row){
friend =[self.friendList objectAtIndex:indexPath.row];
}
PFUser *acceptedFriend = nil;
if ([self.accepted count] > 0){
acceptedFriend = [self.accepted objectAtIndex:indexPath.row];
}
switch (indexPath.section) {
case 0:
if ([self.friendList count] > indexPath.row){
cell.textLabel.text = friend.username;
cell.detailTextLabel.text= friend.email;
cell.imageView.image = [UIImage imageNamed:@"Treehouse.png"];
}
break;
case 1:
if ([self.accepted count] >indexPath.row){
cell.textLabel.text = acceptedFriend.username;
cell.detailTextLabel.text = acceptedFriend.email;
cell.imageView.image = [UIImage imageNamed:@"Treehouse.png"];
}
default:
cell.textLabel.text = @"Nothing Here";
break;
}
// Configure the cell...
return cell;
}
第一部分中没有显示数据,但第二部分有一个单元格列表,文字" Nothing Here" ,但是图片和{{ 1}}' s正确显示。我希望在适当的部分填写数据,并在每个单元格中使用正确的名称。
编辑:我更改了friendList的数据源,它确实在其部分中显示数据。现在出现的问题是第2节显示默认值,但第1节没有显示默认值。
这是请求的numberOfRowsInSection方法
detailTextLabel
答案 0 :(得分:0)
您的问题可能是因为您使用错误的方法设置数组:
-(void)viewDidAppear:(BOOL)animated
该方法发生在诸如numberOfRowsInSection和cellForRowAtIndexPath之类的UITableView方法运行后...
所以这意味着你可能会在那里得到空阵列......
尝试进行:
- (void)viewDidLoad
但如果你仍然希望它留在viewDidAppear方法中添加
[self.tableView reloadData];
更新tableView后的代码