我正在尝试使用 iOS 学习 json。我正在尝试使用.json实现json数据UITableView
。在我的标题和副标题的 tableview 行中显示值。我可以在标题中加载数据但是当尝试加载字幕时,应用程序崩溃了,因为在字幕结果中给出了 json 字段和许多键对值。所以不可能在tableview的副标题中显示。
我的问题是如何加载副标题,以及如果单击时可以使用多个键对值中的键,它会被重定向到其他 tableview 以显示该数据。
我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://query.yahooapis.com/v1/public/yql?q=select+*+from+weather.forecast+where+woeid%3D1100661&format=json"]];
NSError *error=nil;
id response=[NSJSONSerialization JSONObjectWithData:data options:
NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"%@",[error localizedDescription]);
} else {
_query = [response objectForKey:@"query"];
NSLog(@"%@",_query);
_keyArray = [_query allKeys];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_keyArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;
}
NSString *key = [_keyArray objectAtIndex:indexPath.row];
//NSString *dictionary = [_query objectForKey:key];
NSString *dictionary = [_query objectForKey:key];
cell.textLabel.text = key;
cell.detailTextLabel.text = dictionary;
return cell;
}
谢谢。
我从你的问题和评论中了解到的事情,这是我一步一步的回答......
首先,我在班级中采用了 aNSArray
和 aNSDictionary
属性ViewController.h
。
{
NSArray *_allKeys;
}
@property(nonatomic,strong) NSDictionary *query;
然后,在ViewController.m
我已经创建了一个setter方法query
属性,在这里我设置的数据为_query
和_allKeys
这样的...
-(void)setQuery:(NSDictionary *)query
{
_query = query;
_allKeys = [_query allKeys];
if ([self isViewLoaded]) {
[tableView reloadData];
}
}
现在在UITableView
数据源方法中cellForRow
,我更新了您的代码..
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
NSString *key = _allKeys[indexPath.row];
id value = _query[key];
cell.textLabel.text = key;
if ([value isKindOfClass: [NSString class]]) {
cell.accessoryType = UITableViewCellAccessoryNone;
cell.detailTextLabel.text = (NSString*)value;
}
else if ([value isKindOfClass: [NSDictionary class]])
{
cell.detailTextLabel.text = @"More Info";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else if ([value isKindOfClass: [NSArray class]])
{
cell.detailTextLabel.text = @"Multiple Entries Found";
cell.accessoryType = UITableViewCellAccessoryNone;
// do something show array data with prototype custom cell
}
return cell;
}
现在在UITableView
委托方法中didSelect
,我为相同的对象创建了一个新实例ViewController
(我们可以重用它,因为它具有相同的 UI 布局),并传递_query
...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = _allKeys[indexPath.row];
id value = _query[key];
if ([value isKindOfClass: [NSDictionary class]])
{
ViewController *nextVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
nextVC.query = value;
[self.navigationController pushViewController:nextVC animated:YES];
}
}
注意:每当我实例化实例时,我都不想调用该webservice
调用。所以我把代码放在方法中。ViewController.m
ViewController
webservice
AppDelegate
didFinishLaunch
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://query.yahooapis.com/v1/public/yql?q=select+*+from+weather.forecast+where+woeid%3D1100661&format=json"]];
NSError *error=nil;
id response=[NSJSONSerialization JSONObjectWithData:data options:
NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"%@",[error localizedDescription]);
} else {
NSDictionary* _query = [response objectForKey:@"query"];
dispatch_async(dispatch_get_main_queue(), ^{
ViewController *vc = (ViewController*)[(UINavigationController*)self.window.rootViewController topViewController];
vc.query = _query;
});
}
// Override point for customization after application launch.
return YES;
}
放置此代码的位置取决于您,但最好我们应该将以下代码放在UIViewController
您的前一个DatashowingViewController
(我的是ViewController
)并将信息传递给ViewController
(就像我所做的那样),以便我们可以重复使用相同的代码UIViewController
来显示相同的内容结果。
我希望这个答案能帮助你实现你的输出。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句