【问题标题】:TableView Array returns nullTableView 数组返回 null
【发布时间】:2016-07-05 07:55:59
【问题描述】:

我在ViewController 中实现了UITableview。我想在通过JSON 获取的TableView 中显示一些数据。

- (void)getData
{

   NSURLSession*session=[NSURLSession sharedSession];

   NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

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

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


    for (NSDictionary*appDict in entryarr) {
        NSString*title=[[appDict objectForKey:@"im:name"]objectForKey:@"label"];


        NSString*imageStr=[[[appDict objectForKey:@"im:image"]objectAtIndex:2]objectForKey:@"label"];
        NSURL*imageURL=[NSURL URLWithString:imageStr];

        NSData*imageData=[[NSData alloc]initWithContentsOfURL:imageURL];
        [self.imageArray addObject:imageData];
        [self.tittleArray addObject:title];
        [self.termArray addObject:title];

    }

    NSLog(@"%lu %lu %lu",(unsigned long)self.imageArray.count,(unsigned long)self.tittleArray.count,(unsigned long)self.termArray.count);

}];
[dataTask resume];

}

现在,当我将数组分配给 TableView 单元格时,它给出的数组是空的。 但在 getData 方法中,Array 由 10 个元素组成。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _tittleArray.count;


}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString*reUse=@"use";
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:reUse];

    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reUse];
        cell.textLabel.textColor = [UIColor orangeColor];


    }
    cell.textLabel.text=[self.tittleArray objectAtIndex:indexPath.row];

    cell.imageView.image=[self.imageArray objectAtIndex:indexPath.row];

    return cell;

}

为什么会这样。?我该怎么做。?谁能给我有效的解决方案?

【问题讨论】:

  • reloadData is missing ... .reload your tableview
  • 不是三个单独的数组使用一个数组和一个包含三个属性的自定义类作为数据模型。这是面向对象的编程。
  • 结帐给出答案。

标签: ios objective-c json uitableview


【解决方案1】:

好吧,虽然所有其他答案都建议添加

[self.tableView reloadData];

如果您的块在主线程中运行,则可能已经解决了问题,但由于您正在执行一个转到另一个线程的网络操作,您将无法从该线程修改 UI。您可能想要这样做:

dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
});

首先,这是一个根本问题,您甚至没有调用 reload 函数。另一个问题是对事物如何运作的基本理解。因此,无论何时,您的模型已准备好并已填充但您的 UI 没有更改(假设您调用函数来更改 UI),请尝试查看日志输出,有时它会说您无法在另一个线程中更改 UI 并检查哪个您的代码正在运行的线程。

【讨论】:

    【解决方案2】:

    只需从dataTaskWithURL 的完成处理程序重新加载您的tableView

    并等待Web服务调用完成,因为它是异步调用,所以你的tableview的reloaddata会在一段时间后被调用。如果数据较大或互联网连接速度较慢,则可能需要很长时间。

    您可以在完成处理程序中放置断点以检查它何时被调用。

    您也可以显示活动指示器。

    第二件事要确保你的 title、imageStr 或 imageData 不是 nil 或 null。因为您只是在打印数组计数。如果你在数组中添加空对象,那么它会增加计数,但它的实际值将是空白的。

    所以,正确地管理所有事情,当你做错的时候你会发现的。

    【讨论】:

      【解决方案3】:

      如果数组计数大于 0,则调用重新加载数据 请参阅下面的代码以供参考

      -(void)getData {

      NSURLSession*session=[NSURLSession sharedSession];

      NSURLSessionDataTask *dataTask = [会话 dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

      NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
      NSLog(@"%@", json);
      
      NSArray*entryarr=[[json objectForKey:@"feed"]objectForKey:@"entry"];
      
      
      for (NSDictionary*appDict in entryarr) {
          NSString*title=[[appDict objectForKey:@"im:name"]objectForKey:@"label"];
      
      
          NSString*imageStr=[[[appDict objectForKey:@"im:image"]objectAtIndex:2]objectForKey:@"label"];
          NSURL*imageURL=[NSURL URLWithString:imageStr];
      
          NSData*imageData=[[NSData alloc]initWithContentsOfURL:imageURL];
          [self.imageArray addObject:imageData];
          [self.tittleArray addObject:title];
          [self.termArray addObject:title];
      
      }
      
      
      NSLog(@"%lu %lu %lu",(unsigned long)self.imageArray.count,(unsigned long)self.tittleArray.count,(unsigned long)self.termArray.count);
      
       }];
       [dataTask resume];
      
       // add reload data
       if([self.termArray count] >0)
      // call reload data here
       }
      

      【讨论】:

        【解决方案4】:

        在调用 API 之前,只需编写数组对象的 like、alloc 和 init 对象。

        喜欢,

        NSMutableArray *titleArray = [[NSMutableArray alloc] init]; 
        

        对于您用于显示记录的 CellForRowAtIndexPath 方法中的所有数组。

        并在从 Web 服务获取所有记录后重新加载您的表视图数据。

        注意:根据您的问题,您得到的是空数组,因此请检查给定的解决方案。

        【讨论】:

        • 他还说他之前的计数是10。
        【解决方案5】:

        在块内重新加载表格

            NSURLSession*session=[NSURLSession sharedSession];
        
            NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        
                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                NSLog(@"%@", json);
        
                NSArray*entryarr=[[json objectForKey:@"feed"]objectForKey:@"entry"];
        
        
                for (NSDictionary*appDict in entryarr) {
                    NSString*title=[[appDict objectForKey:@"im:name"]objectForKey:@"label"];
        
        
                    NSString*imageStr=[[[appDict objectForKey:@"im:image"]objectAtIndex:2]objectForKey:@"label"];
                    NSURL*imageURL=[NSURL URLWithString:imageStr];
        
                    NSData*imageData=[[NSData alloc]initWithContentsOfURL:imageURL];
                    [self.imageArray addObject:imageData];
                    [self.tittleArray addObject:title];
                    [self.termArray addObject:title];
        
                }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView Reloaddata];
        });
        
                NSLog(@"%lu %lu %lu",(unsigned long)self.imageArray.count,(unsigned long)self.tittleArray.count,(unsigned long)self.termArray.count);
            }]; [dataTask resume];
        
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-08-11
          • 1970-01-01
          • 2015-01-21
          • 2016-04-09
          • 1970-01-01
          • 1970-01-01
          • 2013-05-15
          相关资源
          最近更新 更多