在多节表视图中发送无法识别的选择器的字符串

时间:2014-05-08 05:06:11

标签: ios parse-platform

由于未捕获的异常'NSInvalidArgumentException',我收到 *终止应用程序,原因:' - [__ NSCFConstantString用户名]:无法识别的选择器发送到实例0x1e2460' * 首先抛出调用堆栈:

此错误源自

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    PFUser *friend = nil;
    if ([self.friendList count]> indexPath.row){
        friend =[self.friendList objectAtIndex:indexPath.row];
    }

    PFUser *acceptedFriend = nil;
    if ([self.accepted count] > indexPath.row){
       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){
            //APP crashes here
            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;
}

崩溃指向cell.textLabel.text = acceptedFriend.username;

的实例

viewDidAppear方法是从解析中发生查询并使用解析数据设置数据源的地方。

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    /*
    PFRelation *friendRelation = [self.currentUser relationForKey:@"friendRelation"];
    PFQuery *existingFriends = [friendRelation query];
    [existingFriends findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error){
            NSLog(@"Error: %@, %@", error, [error userInfo]);
        }else{
            self.friendList = objects;
            [self.tableView reloadData];
        }
    }];
     */

    PFQuery *userQuary = [PFUser query];
    PFQuery *acceptedQuary = [PFUser query];
    PFQuery *pendingFriends = [PFQuery queryWithClassName:@"FriendRequest"];
    PFQuery *aceptedFriends = [PFQuery queryWithClassName:@"FriendRequest"];
    [aceptedFriends whereKey:@"toUser" equalTo:self.currentUser.objectId];
    [aceptedFriends whereKey:@"status" equalTo:@"Accepted"];
    [acceptedQuary whereKey:@"objectId" matchesKey:@"fromUser" inQuery:aceptedFriends];
    [pendingFriends whereKey:@"status" doesNotMatchKey:@"status" 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;

            //delte me
            //self.accepted =objects;
            NSLog(@"%@", self.friendList);
            [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];
        }
    }];



}

我还提供了数据,它在对象中确实有用户名数据密钥。

编辑: 我现在已经设置了两个部分来显示相同​​的数据,现在我收到一个 *终止应用程序,因为未捕获的异常'NSRangeException',原因:'* - [__ NSArrayM objectAtIndex:]:index 0超出空数组的边界' * 首先抛出调用堆栈:错误。它发生在

周围
  PFUser *acceptedFriend = nil;
    if ([self.accepted count] > indexPath.row){
       acceptedFriend = [self.friendList objectAtIndex:indexPath.row];

它在名为的方法中发生      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

2 个答案:

答案 0 :(得分:0)

您的错误告诉您,您正尝试在username对象上调用NSString方法。您在两种情况下致电usernamefriendacceptedfriend。因此,您应该确认这两行中的哪一行导致了问题(您可以通过添加exception breakpoint来完成此操作)。

然后您可以确认:

  • 您从数组中检索的对象实际上是NSString对象,而不是具有PFUserusername属性的email对象;和

  • 然后你就可以确认你的数组为什么有字符串而不是自定义对象。

您正在friendlist中记录acceptedviewDidLoad个数组。这些数组在那里看起来是否正常(即它们是自定义对象的数组还是简单的NSString个对象)?

答案 1 :(得分:0)

问题是你的tableView是在viewDidLoad上加载的,而数据是在viewDidAppear中传递给你的字符串的。解决方案是,只有在要加载数据时才需要尝试加载tableView,即从解析中获取详细信息。为此,在numberOfRows方法中,您最初可以返回0,一旦获得详细信息,您可以返回详细信息数组计数。(仅作为示例)