我正在尝试用iOS学习json。我正在尝试用json实现UITableView
数据。在我的tableview中,Title和subtitle中的行显示了值。我可以在标题中加载数据但是当尝试加载字幕时,应用程序正在崩溃,因为在字幕结果中,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;
}
谢谢。
我从你的问题和评论中理解的事情,这是我的答案一步一步......
首先,我在我的NSArray
类中采用了NSDictionary
和ViewController.h
属性。
{
NSArray *_allKeys;
}
@property(nonatomic,strong) NSDictionary *query;
然后在ViewController.m
我为query
属性创建了一个setter方法,我将数据设置为_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
显示相同的结果。
我希望这个答案可以帮助您实现输出。