【发布时间】: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;
}
请有人帮帮我。提前致谢。我是编程新手。
【问题讨论】:
-
您应该返回 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