当为tableView完成ReloadData时,应用程序崩溃

时间:2013-03-15 21:28:15

标签: ios uitableview afnetworking

我使用以下代码重新加载我的tableView,当它完成将所有数据添加到我的Mutable Arrays时,但应用程序总是崩溃

   - (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view.

    //Spinner Add while waiting
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    spinner.frame = CGRectMake(147, 10, 25, 25);
    [self.tableView addSubview:spinner];
    [spinner startAnimating];

    operationQueue = [[NSOperationQueue alloc] init];

    usersFirstName = [[NSMutableArray alloc] init];
    usersLastName = [[NSMutableArray alloc] init];
    usersAvatar = [[NSMutableArray alloc] init];

    NSString *urlstring = [NSString stringWithFormat:@"https://www.test.com/scribble/%@/",scribbleId];
    NSURL *url = [NSURL URLWithString:urlstring];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
        users = JSON[@"users_favored"];
        if (users.count > 0) {
            NSUInteger count = [users count];
            for (NSUInteger i =0; i<count; i++) {
                NSString *urlString = [NSString stringWithFormat:@"https://www.test.com%@",[users objectAtIndex:i]];
                NSURL *url = [NSURL URLWithString:urlString];
                NSURLRequest *requestUser = [NSURLRequest requestWithURL:url];
                AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:requestUser success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
                    if (i == 0) {
                        usersFirstName = [JSON[@"first_name"] mutableCopy];
                        usersLastName = [JSON[@"last_name"] mutableCopy];
                        usersAvatar = [JSON[@"user_avatar"] mutableCopy];
                    }else{
                        [usersFirstName addObjectsFromArray:JSON[@"first_name"]];
                        [usersLastName addObjectsFromArray:JSON[@"last_name"]];
                        [usersAvatar addObjectsFromArray:JSON[@"user_avatar"]];
                    }
                    if (i == count-1) {
                        [self.tableView reloadData];
                        [spinner stopAnimating];
                        [spinner removeFromSuperview];
                    }
                }failure:nil];
                [operationQueue addOperation:operation];
            }
        }else{
            self.navigationItem.title = @"No Favors";
            [spinner stopAnimating];
            [spinner removeFromSuperview];
        }
    } failure:nil];
    [operationQueue addOperation:operation];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return usersFirstName.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableFavorIdentifier = @"FavorCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableFavorIdentifier];

    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableFavorIdentifier];
    }

    NSString *firstName = [usersFirstName objectAtIndex:indexPath.row];
    NSString *lastName = [usersLastName objectAtIndex:indexPath.row];
    NSString *avatar = [usersAvatar objectAtIndex:indexPath.row];
    NSString *userFullName = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
    UIImageView *userAvatar = (UIImageView *)[cell viewWithTag:100];
    if ([avatar length ] > 0) {
        NSString *img = [@"https://dtest_media_and_assets.s3.amazonaws.com/" stringByAppendingString:avatar];
        [userAvatar setImageWithURL:[NSURL URLWithString:img] placeholderImage:[UIImage imageNamed:@"scribble.png"]];
    }else{
        userAvatar.image = [UIImage imageNamed:@"scribble.png"];
    }
    userAvatar.layer.cornerRadius = 4.0;
    userAvatar.clipsToBounds = YES;
    UILabel *userNameLabel = (UILabel *)[cell viewWithTag:101];
    userNameLabel.text = userFullName;

    return cell;

}

它给出的错误是

 2013-03-15 16:37:08.910 test[19547:c07] -[__NSCFString count]: unrecognized selector sent to instance 0x8679c80

2013-03-15 16:37:08.911 test[19547:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString count]: unrecognized selector sent to instance 0x8679c80'

*** First throw call stack:
(0x1cdf012 0x169fe7e 0x1d6a4bd 0x1ccebbc 0x1cce94e 0x143d7 0x7c8548 0x7cb224 0x68f952 0x68f2dc 0x140d7 0x281d2 0x29569 0x2f9553f 0x2fa7014 0x2f977d5 0x1c85af5 0x1c84f44 0x1c84e1b 0x21bb7e3 0x21bb668 0x5e3ffc 0x200d 0x1f35)
libc++abi.dylib: terminate called throwing an exception

修改

当我使用它时,一切正常

[usersFirstName addObject:JSON[@"first_name"]];
[usersLastName addObject:JSON[@"last_name"]];
[usersAvatar addObject:JSON[@"user_avatar"]];
[self.tableView reloadData];
if (i == count-1) {
    [spinner stopAnimating];
    [spinner removeFromSuperview];
}

2 个答案:

答案 0 :(得分:2)

尝试使用该功能:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"usersFirstName is of type %@", NSStringFromClass([usersFirstName class]));
    return [usersFirstName count]; 
}

我想你必须更换:

          if (i == 0) {
                    usersFirstName = [JSON[@"first_name"] mutableCopy];
                    usersLastName = [JSON[@"last_name"] mutableCopy];
                    usersAvatar = [JSON[@"user_avatar"] mutableCopy];
                }else{
                    [usersFirstName addObjectsFromArray:JSON[@"first_name"]];
                    [usersLastName addObjectsFromArray:JSON[@"last_name"]];
                    [usersAvatar addObjectsFromArray:JSON[@"user_avatar"]];
                }

只是:

                    [usersFirstName addObject:JSON[@"first_name"]];
                    [usersLastName addObject:JSON[@"last_name"]];
                    [usersAvatar addObject:JSON[@"user_avatar"]];

答案 1 :(得分:1)

问题在于:

users = JSON[@"users_favored"];

当看起来您期待NSString时,会返回NSArray

使用以下方式验证:

users = JSON[@"users_favored"];
NSLog(@"users is of type %@", NSStringFromClass([users class]));

你的假设有问题或者JSON坏了。