0

当我将所有数据添加到我的可变数组时,我正在使用以下代码重新加载我的 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;

}

它给出的错误是

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

2 回答 2

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"]];
于 2013-03-15T21:56:48.573 回答
1

问题在于:

users = JSON[@"users_favored"];

返回一个NSString,当它看起来你期待一个NSArray

使用以下方法验证这一点:

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

您的假设有问题或 JSON 已损坏。

于 2013-03-15T21:43:03.520 回答