如何检查密钥对中的密钥是否可用?

问题描述 投票:0回答:1

我正在尝试用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; 
}

谢谢。

ios objective-c json uitableview nsdictionary
1个回答
1
投票

我从你的问题和评论中理解的事情,这是我的答案一步一步......

首先,我在我的NSArray类中采用了NSDictionaryViewController.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显示相同的结果。

我希望这个答案可以帮助您实现输出。

© www.soinside.com 2019 - 2024. All rights reserved.