ios - 数据不会从 JSON 解析加载到某些 Tableview 单元格上

标签 ios objective-c iphone json uitableview

我在 XCODE 7.1 上制作简单的应用程序。 我只在 tableview 单元格中显示 2 个标签和 1 个图像。我正在从这个 URL 解析数据.我只是简单地在 Tableview 中加载数据在这里我放了 ViewController.m 文件的代码

    @interface ViewController ()

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityIndicator.alpha = 1.0;
    [self.view addSubview:activityIndicator];
    activityIndicator.center = CGPointMake([[UIScreen mainScreen]bounds].size.width/2, [[UIScreen mainScreen]bounds].size.height/2);
    [activityIndicator startAnimating];//to start animating
    // Do any additional setup after loading the view, typically from a nib.


    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];


    NSURL *URL = [NSURL URLWithString:@"http://www.androidbegin.com/tutorial/jsonparsetutorial.txt"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            [activityIndicator stopAnimating];
            _responsedic = (NSDictionary*) responseObject;
            _Worldpopulation = [_responsedic valueForKey:@"worldpopulation"];
            _imageURL = [_Worldpopulation valueForKey:@"flag"];
            _country = [_Worldpopulation valueForKey:@"country"];
            _population = [_Worldpopulation valueForKey:@"population"];
            NSLog(@"Data:%@",_imageURL);
            NSLog(@"Population",_population);
            NSLog(@"Country",_country);
           // NSLog(@"%@",_MovieList);
            //NSLog(@"Array: %@",_imageURL);
            //NSLog(@"%@",responseObject);
        }
    }];
    [dataTask resume];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *Identifier = @"mycell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Identifier];
    // Set and load the images
    [cell.imageView sd_setImageWithURL:[_imageURL objectAtIndex:indexPath.row] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        // Get rid of the activity indicator when the image has been loaded
    }];




    cell.textLabel.text = [_country objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [_population objectAtIndex:indexPath.row];
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //NSString *rowValue = self.friends[indexPath.row+1];
    NSString *message = [[NSString alloc] initWithFormat:@"You selected %@",[_country objectAtIndex:indexPath.row]];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"YOU SELECT"
                                                    message:message
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

我正在使用 AFNetworking 3.0 和 SDWebImage 进行图像加载。数据解析成功并显示在 tableview 中。我在下面附上截图

Simulator screenshot

问题是所有数据都没有显示在 tableview 单元格中我还在成功加载但未显示在单元格中的 tableview 数据的每个单元格上放置了警报对话框。我到处搜索我找不到解决方案我正在使用 3G 连接所以网速不是问题请有人帮助。

最佳答案

尝试在完成 block 中使用更新后的数据重新加载 TableView 。

NSURL *URL = [NSURL URLWithString:@"http://www.androidbegin.com/tutorial/jsonparsetutorial.txt"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        [activityIndicator stopAnimating];
        _responsedic = (NSDictionary*) responseObject;
        _Worldpopulation = [_responsedic valueForKey:@"worldpopulation"];
        _imageURL = [_Worldpopulation valueForKey:@"flag"];
        _country = [_Worldpopulation valueForKey:@"country"];
        _population = [_Worldpopulation valueForKey:@"population"];
        NSLog(@"Data:%@",_imageURL);
        NSLog(@"Population",_population);
        NSLog(@"Country",_country);
       // NSLog(@"%@",_MovieList);
        //NSLog(@"Array: %@",_imageURL);
        //NSLog(@"%@",responseObject);


        //Added Code -> Reloading data on Main queue for update
        dispatch_async(dispatch_get_main_queue(), ^{

            [self.tableview reloadData];
        }); 
    }
}];
[dataTask resume];

希望对您有所帮助。
谢谢。

关于ios - 数据不会从 JSON 解析加载到某些 Tableview 单元格上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34848290/

相关文章:

ios - AudioUnit + Opus 编解码器 = 裂纹问题

iphone - 如何格式化 UIDatepiker 日期?

ios - 用于释放密码的 iPhone 内存管理(生产中的 Malloc Scribble?,用零填充释放内存?)

iphone - 构建应用程序时,Xcode 会编译未使用的类吗?

iphone - 将 UIImageView 添加到 UIView

ios - 图像重叠 ui 导航栏 swift

ios - 异步任务完成时的返回值

iphone - iPhone应用程序Mapkit中多个点之间的最短路线

iphone - 单击 UITableViewCell 中的 UITextField

android - React Native - 导航到屏幕 - 无效的 Hook 调用