在cellForRowAtIndexPath之后调用ViewDidLoad成功块

时间:2014-04-29 20:54:36

标签: ios

我有一个api调用将一些数据存储到表视图的数组属性中。

我在viewDidLoad中拨打电话,除非在成功之前运行api呼叫直到线路,否则一切顺利。

然后它跳转到cellForRowAtIndexPath,我将调用后的数据设置为NSDictionary,然后在该行之后,它不会完成cellForRowAtIndexPath方法。它会重新启动并运行api调用的成功代码,然后再返回cellForRowAtIndexPath

它在viewDidLoad中获取的行是成功块之前的行:

[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)

然后跳转到cellForRowAtIndexPath中的这一行:

NSDictionary *user = self.user[0];

以下是我的完整代码:

viewDidLoad方法:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.tableFooterView = [[UIView alloc] init];

    GFCredentialStore *credentialStore = [[GFCredentialStore alloc] init];

    NSString *authToken = [credentialStore authToken];
    NSLog(@"%@", authToken);

    __weak typeof(self)weakSelf = self;

    NSString *userID = self.userID;

    NSString *urlString = [NSString stringWithFormat:@"%s%s%@%@", kBaseURL, kUserURL, userID, @".json"];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [GFUserResponseSerializer serializer];
    [manager.requestSerializer setValue:authToken forHTTPHeaderField:@"auth_token"];
    NSLog(@"%@", manager.requestSerializer.HTTPRequestHeaders);
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        __strong typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.user = (NSArray *)responseObject;
        [strongSelf.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

cellForRowAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if(indexPath.section == 0){

        GFProfileCell *cell = [tableView dequeueReusableCellWithIdentifier:@"profileCell" forIndexPath:indexPath];

        NSDictionary *user = self.user[0];

        cell.usernameLabel.text = user[@"username"];

        cell.bioLabel.text = user[@"description"];

        NSString * avatarURL = [NSString stringWithFormat:@"%s%s%@%s%@%s", kBaseURL, "system/users/avatars/", user[@"id"], "/original/", user[@"username"], ".png"];

        NSString * newAvatarURL = [avatarURL stringByReplacingOccurrencesOfString:@" " withString:@"_"];

        [cell.avatarImage setImageWithURL:[NSURL URLWithString:newAvatarURL] placeholderImage:[UIImage imageNamed:@"Zoo.png"]];

        cell.avatarImage.layer.cornerRadius = 40;
        cell.avatarImage.layer.masksToBounds = YES;

        [cell.followingCountBtn addTarget:self action:@selector(followingBtnClick:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
        [cell.followerCountBtn addTarget:self action:@selector(followerBtnClick:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];

        // edit profile becomes follow
        cell.editProfileButton.layer.cornerRadius = 3;
        cell.editProfileButton.layer.borderColor = UIColorFromRGB(0x1FAA4E).CGColor;
        cell.editProfileButton.layer.borderWidth = 1.0f;
        cell.editProfileButton.titleLabel.textColor = UIColorFromRGB(0x1FAA4E);

        return cell;

    }
    else if(indexPath.section == 1){

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

        return cell;

    }
    else {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"postsCell" forIndexPath:indexPath];

        return cell;

    }
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

您正在使用的API(AFNetworking)是异步的,因此成功块自然只会在从网络接收到数据后执行。

与此同时,tableview已经开始加载您的数据。

在此期间,您可以显示进度指示器(或加载动画),或者您可以预先加载数据(如果可能)(似乎您正在为登录用户加载数据,因此它可能是无论如何,当你的应用程序启动时加载这个好主意。)