Tableview数据通过Model Class

时间:2016-07-05 17:48:47

标签: ios objective-c uitableview

我在VC1中实现了UITableview,我想在单元格中显示一些JSON数据。我已经实现了一个Model类来将数据传递给表视图。

dispatch_async(dispatch_get_main_queue(), ^{

NSURLSession*session=[NSURLSession sharedSession];

NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/search?term=music"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSLog(@"%@", json);

    NSArray *entryarr = [json objectForKey:@"results"];

    TableClass *tc = [[TableClass alloc] init];

    for (NSDictionary *appDict in entryarr) {


        //setting title

        NSString *str = [appDict objectForKey:@"artistName"];
        [tc setTittle:str];

        NSLog(@"artist Name=%@",tc.tittle);

        //setting Subtitle
        NSString *sub = [appDict objectForKey:@"country"];

        [tc setSubtittle:sub];

        NSLog(@"artist Name=%@",tc.subtittle);

        //image

        NSString *imageStr = [appDict objectForKey:@"artworkUrl60"];


        NSURL *imageURL = [NSURL URLWithString:imageStr];

        [tc setImage:imageStr];



        NSData *imageData  =[[NSData alloc] initWithContentsOfURL:imageURL];

        //[self.imageArray addObject:imageData];

        [_tableArray addObject:tc];

        NSLog(@"%@ name of tittle",[_tableArray objectAtIndex:0]);

    }

    NSLog(@"%lu %lu %lu",(unsigned long)self.tableArray.count,(unsigned long)self.tableArray.count,(unsigned long)self.tableArray.count);

    [self.tableView reloadData];

 }];
    [dataTask resume];
});

但是,在访问单元格时,我得到了数组中的最后一个元素。

TableClass *tableclassModel = [self.tableArray objectAtIndex:indexPath.row];
cell.textLabel.text = tableclassModel.tittle;
cell.detailTextLabel.text = tableclassModel.subtittle;
cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:tableclassModel.image]]];

为什么会这样......?我该怎么办?

1 个答案:

答案 0 :(得分:0)

您正在反复更改同一 tc 对象的值。由于你已经在for循环之外声明了 tc 对象,因此只有一个tc。在循环的第一次迭代中进行所需的更改后,您将其添加到_tableArray。在第二次迭代中,您将更改第一次迭代中使用的相同tc对象的值,并再次将其添加到_tableArray。这将使用新值更新第一个对象。这继续,最后你的_tableArray将包含n个具有相同值的tc对象(即最后更新的值)

在for循环中给出声明

dispatch_async(dispatch_get_main_queue(), ^{

NSURLSession*session=[NSURLSession sharedSession];

NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/search?term=music"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSLog(@"%@", json);

    NSArray*entryarr=[json objectForKey:@"results"];

    for (NSDictionary*appDict in entryarr) {

        TableClass*tc=[[TableClass alloc]init];

        //setting tittl

        NSString*str=[appDict objectForKey:@"artistName"];
        [tc setTittle:str];

        NSLog(@"artist Name=%@",tc.tittle);

        //setting Subtittle
        NSString*sub=[appDict objectForKey:@"country"];

        [tc setSubtittle:sub];

        NSLog(@"artist Name=%@",tc.subtittle);

        //image

        NSString*imageStr=[appDict objectForKey:@"artworkUrl60"];


        NSURL*imageURL=[NSURL URLWithString:imageStr];

        [tc setImage:imageStr];



        NSData*imageData=[[NSData alloc]initWithContentsOfURL:imageURL];

        //[self.imageArray addObject:imageData];

        [_tableArray addObject:tc];

        NSLog(@"%@ name of tittle",[_tableArray objectAtIndex:0]);

    }

    NSLog(@"%lu %lu %lu",(unsigned long)self.tableArray.count,(unsigned long)self.tableArray.count,(unsigned long)self.tableArray.count);

    [self.tableView reloadData];

 }];
[dataTask resume];
});