ios - 在 cellForRowAtIndexPath 之后调用的 ViewDidLoad 成功 block

标签 ios

我有一个 api 调用,用于将一些数据存储到 TableView 的数组属性中。

我在 viewDidLoad 中进行调用,一切顺利,除了它在成功之前将 api 调用运行到该行。

然后它跳转到 cellForRowAtIndexPath 到我设置从调用 NSDictionary 获得的数据的地方然后在那一行之后它没有完成 cellForRowAtIndexPath 方法。它会跳回并运行 api 调用的成功代码,然后再返回到 cellForRowAtIndexPath

它在 viewDidLoad 中到达的那一行是成功 block 之前的那一行:

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

    }
}

感谢您的帮助。

最佳答案

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

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

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

关于ios - 在 cellForRowAtIndexPath 之后调用的 ViewDidLoad 成功 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23375059/

相关文章:

iOS - 延迟向 UITableView 添加行的最佳方法?

ios - arc4random 函数随机执行两次或三次

ios - 下载图像时检查 Nil 后变量显示 Nil

ios - 使 UITableView 行可选择

ios - 为什么我看不到 UIPageControl?

ios - 试图更改 UISwitch 的背景图像

ios - 我可以在没有通知许可的情况下使用 FCM 接收消息吗?

ios - 在 XCTestCase 中使用 UIImage imageNamed

ios - 如何使用一个 View Controller 上的按钮将文本添加到另一个 View Controller 的 TableView 中的一行?

ios - 在 Swift 中使用 AnyObjects 对数组进行排序