Ios 为tableView重新加载数据时,应用程序崩溃

Ios 为tableView重新加载数据时,应用程序崩溃,ios,uitableview,afnetworking,Ios,Uitableview,Afnetworking,在将所有数据添加到可变数组时,我使用以下代码重新加载tableView,但应用程序总是崩溃 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //Spinner Add while waiting UIActivityIndicatorView *spinner = [[UIActivityIndicatorView

在将所有数据添加到可变数组时,我使用以下代码重新加载tableView,但应用程序总是崩溃

   - (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;

}
编辑 当我用这个替代时,一切都很好

[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];
}

问题在于:

users = JSON[@"users_favored"];
返回一个
NSString
,当它看起来您需要一个
NSArray

使用以下方法验证此操作:

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

您的假设有问题,或者JSON被破坏。

问题在于:

users = JSON[@"users_favored"];
返回一个
NSString
,当它看起来您需要一个
NSArray

使用以下方法验证此操作:

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

您的假设有问题,或者JSON被破坏。

请尝试使用该函数:

- (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"]];

对该函数尝试相同的操作:

- (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"]];


您发布的所有代码都与错误无关。在哪里调用
count
方法或访问
count
属性,可能问题在于,JSON结果实际上返回的是一个字符串值,而您假定存在一个数组。请发布一个符号stacktrace。@特洛伊木马程序请查看edit@rmaddy当我使用addObject而不是addObjectsFromArray时,它会给我同样的错误,另外,请看一下编辑。您发布的所有代码都与错误有关。在哪里调用
count
方法或访问
count
属性,可能问题在于,JSON结果实际上返回的是一个字符串值,而您假定存在一个数组。请发布一个符号stacktrace。@特洛伊木马程序请查看edit@rmaddy当我使用addObject而不是addObjectsFromArray时,它会给我同样的错误,另外,请看一下编辑,上面说用户是__NSCFArray@Jonathan这是令人惊讶的。我退出了,上面说用户属于__NSCFArray@Jonathan这是令人惊讶的。我出局了。所以这很奇怪,
2013-03-15 16:59:12.523工作液[73966:c07]usersFirstName的类型是\uu-NSArrayM 2013-03-15 16:59:12.789工作液[73966:c07]usersFirstName的类型是\uu-NSCFString
我认为前者来自重新加载之前,而后者则来自于重新加载之后
usersLastName=[JSON[@“last\u-name”]mutableCopy]将字符串设置为变量?因此您将用字符串替换数组。这是我一直在使用的,但它不会将其变成可变数组,而是每次在viewdidload函数中都会生成一个新数组。。。在视图的生命周期内,只应调用一次。。你每次都是什么意思?这很奇怪,
2013-03-15 16:59:12.523工作流体[73966:c07]usersFirstName是\uu nsarray类型的,2013-03-15 16:59:12.789工作流体[73966:c07]usersFirstName是\uu NSCFString类型的,我认为前者来自重新加载之前,后者来自重新加载之后可变拷贝]将字符串设置为变量?因此您将用字符串替换数组。这是我一直在使用的,但它不会将其变成可变数组,而是每次在viewdidload函数中都会生成一个新数组。。。在视图的生命周期内,只应调用一次。。你每次都是什么意思?