Ios ViewDidLoad成功块在cellForRowAtIndexPath之后调用
我有一个api调用来将一些数据存储到表视图的数组属性中 我在Ios ViewDidLoad成功块在cellForRowAtIndexPath之后调用,ios,Ios,我有一个api调用来将一些数据存储到表视图的数组属性中 我在viewDidLoad中进行了调用,除了在成功之前将api调用一直运行到行外,一切都进行得很顺利 然后它跳转到cellForRowAtIndexPath,在那里我将从调用中获得的数据设置为NSDictionary,然后在该行之后,它不会完成cellForRowAtIndexPath方法。它跳回并运行api调用的成功代码,然后再返回到cellforrowatinexpath 它在viewDidLoad中到达的行是成功块前面的一行: [ma
viewDidLoad
中进行了调用,除了在成功之前将api调用一直运行到行外,一切都进行得很顺利
然后它跳转到cellForRowAtIndexPath
,在那里我将从调用中获得的数据设置为NSDictionary
,然后在该行之后,它不会完成cellForRowAtIndexPath
方法。它跳回并运行api调用的成功代码,然后再返回到cellforrowatinexpath
它在viewDidLoad中到达的行是成功块前面的一行:
[manager GET:urlString参数: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);
}];
}
- (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;
}
}
cellForRowAtIndexPath方法:
- (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);
}];
}
- (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
)是异步的,因此成功块自然只能在从网络接收数据后执行
同时,tableview将已经开始加载您的数据
在此期间,您可以显示进度指示器(或加载动画),也可以在可能的情况下提前加载数据(似乎您正在加载登录用户的数据,因此在应用程序启动时加载此数据可能是一个好主意)。您正在使用的API(AFNetworking
)是异步的,因此成功块自然只能在从网络接收到数据后执行
同时,tableview将已经开始加载您的数据
在此期间,您可以显示进度指示器(或加载动画),也可以在可能的情况下提前加载数据(似乎您正在加载登录用户的数据,因此在应用程序启动时加载此数据可能是一个好主意)。您正在使用的API(AFNetworking
)是异步的,因此成功块自然只能在从网络接收到数据后执行
同时,tableview将已经开始加载您的数据
在此期间,您可以显示进度指示器(或加载动画),也可以在可能的情况下提前加载数据(似乎您正在加载登录用户的数据,因此在应用程序启动时加载此数据可能是一个好主意)。您正在使用的API(AFNetworking
)是异步的,因此成功块自然只能在从网络接收到数据后执行
同时,tableview将已经开始加载您的数据
在此期间,您可以显示进度指示器(或加载动画),也可以在可能的情况下提前加载数据(似乎您正在加载登录用户的数据,因此在应用程序启动时加载此数据可能是个好主意).对不起,你到底有什么问题?它崩溃了吗?对不起,你到底有什么问题?它崩溃了吗?对不起,你到底有什么问题?它崩溃了吗?对不起,你到底有什么问题?它崩溃了吗?+1.澄清一下,这不是唯一的问题-这是你需要学习的模式,因为数据可能会很快显示出来,或者它可能显示得很慢,或者根本不显示。(您的视图控制器或表委托方法应该能够判断数据处于哪个状态,并适当地向用户显示。现在您假设数据已经存在。)非常正确。虽然存在同步网络API(例如
NSURLConnection
ssendSynchronousRequest:…
method),在主线程上工作时使用同步网络调用通常不是一个好主意。通常=始终:)@AaronBrager&David ok感谢您的回复和帮助。我如何准确地延迟表格视图CellForRowatineXpath,直到数据完全加载。需要在此视图上进行数据API调用,因为它显示了用户配置文件,用户可以是任何用户。一个简单的方法是按照Aaron的建议执行,并跟踪您的da状态ta在视图控制器中。例如,在进行API调用之前,您可以简单地将标志dataAvailable
设置为NO
,并在numberSectionsInTableView
中返回0,而该标志的计算结果为false。(当然,不要忘记将标志设置为YES
并调用[self.tableView reloadData]
API调用完成时。)+1.为了澄清这一点,这不是唯一的问题-这是一个您需要了解的模式,因为数据可能显示得很快,或者显示得很慢,或者根本不显示。(您的视图控制器或表委托方法应该能够判断数据处于何种状态,并适当地向用户显示。现在您假设数据已经存在。)非常正确。虽然存在同步网络API(例如NSURLConnection
ssendSynchronousRequest:…
method),在主线程上工作时使用同步网络调用通常不是一个好主意。General=always:)@AaronBrager&David ok感谢您的回复和帮助。我如何准确地延迟表格视图CellForRowatineXpath,直到数据完全加载。需要在此视图上进行数据API调用,因为它显示了用户配置文件,用户可以是任何用户。一个简单的方法是按照Aaron的建议执行,并跟踪您的da状态ta在视图控制器中。例如,在进行API调用并返回0之前,您可以简单地将标志dataAvailable
设置为NO