【问题标题】:Unable to load json data into tableView无法将 json 数据加载到 tableView
【发布时间】:2016-05-04 01:04:26
【问题描述】:

我正在尝试做一个天气应用程序

 #import "LocationTableViewController.h"

@interface LocationTableViewController (){

    NSArray *hourlyData;
    NSArray *dailyData;
}


@end

@implementation LocationTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *str = @"https://api.forecast.io/forecast/cd8edc928426f2ac3e341441c7a9c6d3/37.8267,-122.423";
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];

    NSDictionary *dataFromWeb = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

我获取了一个 json 查询,它提供了每小时和每天的数据,然后将其转换为字典。在这里,我创建了两个字典,即每小时字典和每日字典,其中包含温度、湿度等字段

我的主要目标是通过将字典加载到表格视图中来创建一个使用每小时和每天的字典的天气应用程序。

    NSDictionary *hourlyDict = [dataFromWeb objectForKey:@"hourly"];
    hourlyData = [hourlyDict objectForKey:@"data"];

    NSDictionary *dailyDict = [dataFromWeb objectForKey:@"daily"];
    dailyData = [dailyDict objectForKey:@"data"];

   NSLog(@"%@", [[dailyData objectAtIndex:0] objectForKey:@"humidity"]);
}

到这里,我成功地创建了这两个字典并尝试 NSlog 工作正常的数据。

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

我觉得我的问题从将字典加载到 tableView 开始。

1) 在故事板中,我将表格视图嵌入到 NavigationView 中。 2)我将表格查看内容作为动态协议 3) 将单元格标识符命名为单元格

我认为问题从将数据发送到表中开始。基本上我的应用程序应该包含 3 个部分的摘要、每小时数据和每日数据。但我现在只想尝试每日数据。

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

因为我不想要任何部分,所以我删除了这个 no.of。部分,但它给我带来了错误,所以保留它并返回 0。我也尝试将其设为 1,但应用程序崩溃了。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 NSLog(@"hello");
     return dailyData.count;
}

在这里,我希望将列作为字典中的行数,所以我创建了 dailyData.count。

这里开始主要问题,这个 - (UITableViewCell *)tableView:........ 函数没有被调用,我试图 NSlog 一条消息,它没有显示

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

NSLog(@"hello");

   cell.textLabel.text = [[dailyData objectAtIndex:indexPath.row]objectForKey:@"sunsetTime"];


    return cell;
}

请有人帮帮我。提前致谢。我是编程新手。

这里我附上了Google Drive link for project

【问题讨论】:

  • 您应该返回 1 表示部分的数量,因为您至少需要 1 个部分才能显示一些数据...
  • 我试过了,应用崩溃了
  • 崩溃是怎么回事?
  • 是的,我尝试了相同的方法,当我在 viewdidLoad 中调用时,它会显示它工作正常的字典。但不适用于 tableView
  • [__NSDictionaryM isEqualToString:]:无法识别的选择器发送到实例 0x7facc8446810 2016-05-03 21:16:51.920 JsonTesting[1770:160452] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因: '-[__NSDictionaryM isEqualToString:]: 无法识别的选择器发送到实例 0x7facc8446810' @Cristik

标签: ios objective-c uitableview dictionary


【解决方案1】:

要解决您的问题,您需要将部分数量设置为 1(您必须始终至少有 1 个部分才能显示任何内容)。

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

将节数设置为 1 时出现崩溃的原因是您试图将 NSDictionary 用作 NSString。您需要从 NSDictionary 中获取 NSString。下面的代码将从该行的每日数据中获取摘要并显示摘要。

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

    NSDictionary *rowData = [dailyData objectAtIndex:indexPath.row];
    cell.textLabel.text = [rowData objectForKey:@"summary"];

    return cell;
}

【讨论】:

    【解决方案2】:

    // 部分应该是 1 而不是 0

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

    [dailyData objectAtIndex:indexPath.row] 有以下字典,无法指定为单元格标签文本,您必须为单元格标签文本分配一些字符串值

       {
                    apparentTemperatureMax = "60.49";
                    apparentTemperatureMaxTime = 1462316400;
                    apparentTemperatureMin = "52.96";
                    apparentTemperatureMinTime = 1462276800;
                    cloudCover = "0.92";
                    dewPoint = "50.37";
                    humidity = "0.8100000000000001";
                    icon = "partly-cloudy-day";
                    moonPhase = "0.89";
                    ozone = "351.23";
                    precipIntensity = 0;
                    precipIntensityMax = 0;
                    precipProbability = 0;
                    pressure = "1014.68";
                    summary = "Mostly cloudy throughout the day.";
                    sunriseTime = 1462281126;
                    sunsetTime = 1462331002;
                    temperatureMax = "60.49";
                    temperatureMaxTime = 1462316400;
                    temperatureMin = "52.96";
                    temperatureMinTime = 1462276800;
                    time = 1462258800;
                    visibility = "8.779999999999999";
                    windBearing = 275;
                    windSpeed = "5.83";
                }
    

    cell.textLabel.text = [dailyData objectAtIndex:indexPath.row]; 您正在将字典分配给 tableViewCell 标签。如果您想要特定的键值,请检查以下代码

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
        //Number to Sting --> [Number stringValue]
        cell.textLabel.text = [[[dailyData objectAtIndex:indexPath.row]objectForKey:@"sunsetTime"] stringValue];
    
        return cell;
    }
    

    【讨论】:

    • @Vinod 将浮点数转换为字符串。 sunsetTime 是一个浮点数,所以使用这个 cell.textLabel.text = [[[dailyData objectAtIndex:indexPath.row]objectForKey:@"sunsetTime"] stringValue];
    【解决方案3】:

    我试图将浮点数传递到我的表格单元格文本中,出现错误。我们必须只将文本传递给那个,但我试图插入浮点值。现在我传递了一些文本值,它工作正常

    感谢大家的支持。再次感谢您

    【讨论】:

    • 对不起兄弟,我们可以将任何值传递给 tableview
    【解决方案4】:

    使用下面的代码,你的代码对我有用,

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     NSLog(@"hello");
         return dailyData.count;
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
        NSLog(@"hello %@",[[dailyData objectAtIndex:indexPath.row] objectForKey:@"humidity"]);
    
       cell.textLabel.text = [NSString stringWithFormat:@"%@",[[dailyData objectAtIndex:indexPath.row] objectForKey:@"humidity"]];
    
        return cell;
    }
    

    您的输出如下所示,

    打印湿度值

    希望对你有帮助

    【讨论】:

      【解决方案5】:

      你必须显示不出来数据,因为你的dailyData是一个NSArray!你怎么用一个数组作为字符串来使用?建议你用一个模型,来存储数据。

          NSDictionary *hourlyDict = [dataFromWeb objectForKey:@"hourly"];
          hourlyData = [hourlyDict objectForKey:@"data"];
      
          NSDictionary *dailyDict = [dataFromWeb objectForKey:@"daily"];
          dailyData = [dailyDict objectForKey:@"data"];
      
      
          for (id  obj in hourlyData) {
              newsModel *model=[[newsModel alloc]init ];
              model.name=obj[@"summary"];
              [_dataArray addObject:model];
          }
      
          NSLog(@"%@", [[dailyData objectAtIndex:0] objectForKey:@"humidity"]);
      

      在表视图委托中:

      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
          static NSString *cellIde=@"cell";
          UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIde];
          if (!cell) {
              cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIde ];
      
          }
          newsModel *model=_dataArray[indexPath.row];
          cell.textLabel.text=model.name;
          return cell;
      
      }
      

      【讨论】:

      • 每日数组已经是字典数组。为什么需要在模型中再次存储?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 2016-06-07
      • 2018-12-23
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多