我在单元格中有一个标签,我想用我解析的JSON数组中的数据更新它。问题是,在获得数据后,标签是空白的,只有在我上下滚动表视图后才会显示。我已经尝试过用[self.view setNeedsDisplay];
强制重新加载视图,但它仍然不起作用。我该如何处理?
以下是代码片段,其中包含一些注释:
@implementation outletView
@synthesize outletInfo;
- (void)viewDidLoad
{
[super viewDidLoad];
...
// Do any additional setup after loading the view.
[[AFHTTPRequestOperationManager manager] GET:[SERVER_URL stringByAppendingString:@"api/points"] parameters:[NSDictionary dictionaryWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults] objectForKey:@"authToken"],@"auth_token",nil] success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *outletID = [[outletInfo valueForKeyPath:@"preference"] objectForKey:@"outlet_id"];
NSArray *outletArray = (NSArray*)responseObject;
NSLog(@"array: %@", outletArray);
for(NSDictionary *diction in outletArray) {
NSString *dictionID = [diction objectForKey:@"outlet_id"];
if ([dictionID isEqualToString:outletID]) {
pointOutlet = [diction objectForKey:@"total"];
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error : %@",[error description]);
}];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = indexPath.section==0 ? @"outletViewCell" : @"categoryCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Here is the cell
if(indexPath.section==0){
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[(UIImageView *)[cell viewWithTag:1] sd_setImageWithURL:[NSURL URLWithString:outletInfo[@"backgroundImageUrl"]] placeholderImage:nil];
[(UIImageView *)[cell viewWithTag:2] sd_setImageWithURL:[NSURL URLWithString:outletInfo[@"logoUrl"]] placeholderImage:nil];
[(UILabel *)[cell viewWithTag:3] setText:outletInfo[@"name"]];
[(UILabel *)[cell viewWithTag:4] setText:outletInfo[@"address"]];
//Here is the UILabel that I want to update
[(UILabel *)[cell viewWithTag:6] setText:pointOutlet];
} else {
...
}
return cell;
}
@end
执行以下更改:-
[[AFHTTPRequestOperationManager manager] GET:[SERVER_URL stringByAppendingString:@"api/points"] parameters:[NSDictionary dictionaryWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults] objectForKey:@"authToken"],@"auth_token",nil] success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *outletID = [[outletInfo valueForKeyPath:@"preference"] objectForKey:@"outlet_id"];
NSArray *outletArray = (NSArray*)responseObject;
NSLog(@"array: %@", outletArray);
for(NSDictionary *diction in outletArray) {
NSString *dictionID = [diction objectForKey:@"outlet_id"];
if ([dictionID isEqualToString:outletID]) {
pointOutlet = [diction objectForKey:@"total"];
}
}
//Here you need to call tableView reload. This will reload your tableView and show the label.
[tableView reload];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error : %@",[error description]);
}];
解析响应时,在主线程上重新加载表。
dispatch_async(dispatch_get_main_queue(), ^{
});
重新加载此块中的表。
当数据没有到来,并且您的tableView方法之前调用,从服务器获取所有数据后重新加载表视图时,就会出现这种问题。