我在单元格中有一个标签,我想用我解析的JSON数组中的数据进行更新。问题是获取数据后标签是空白的,只有在我向上和向下滚动tableview后才会显示。我试图用[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
答案 0 :(得分:1)
进行以下更改: -
[[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]);
}];
答案 1 :(得分:0)
解析响应时在主线程上重新加载表。
dispatch_async(dispatch_get_main_queue(), ^{
});
重新加载此块中的表格。
答案 2 :(得分:0)
当数据未来并且你的tableView方法之前调用时会出现这种问题,在从服务器获取所有数据后重新加载表视图。