【问题标题】:NSDictionary to TableViewNSDictionary 到 TableView
【发布时间】:2011-08-07 18:18:29
【问题描述】:

因为我是 Stackoverflow 的新手,所以我还不能评论某人的 anwser。 (我的声誉是 16 ..)。我有一个关于这个 anwser 的问题:How do I put this JSON data into my table view? Please help me, I'm living in a nightmare :)

Fulvio 说你必须使用 [eventNameList addObject:event];[eventNameList objectAtIndex:indexPath.row]; 来存储和获取事件数据但是。 addObject 是一个 NSMutableSet 方法,而 objectAtIndex:indexPath.row 不是。所以我不能使用这种方法从 NSMutableSet 中获取数据。

除此之外,我也不能使用 count 方法。

有什么想法吗?

【问题讨论】:

    标签: objective-c json ios uitableview


    【解决方案1】:

    幸运:)

    假设您可能有 nsmutablearray of nsdictionary。

    在这种情况下,您可以使用以下方式获取数据:

    [dictionary objectforkey:@"key"] objectAtIndex:indexpath.row]
    

    【讨论】:

    • 不起作用:*** -[__NSArrayM valueForKey:]:消息发送到已释放实例 0x4b72c80
    • cell.text=[NSString stringWithFormat:@"%@",[[arrayList objectAtIndex:indexPath.row] objectForKey:@"keyname"]]; (arrayList 是我的 nsmutablearray,keyname 是 key.. 你需要让 arraylist 成为一个类变量.. 这对我来说很好......
    【解决方案2】:

    假设你有一个 NSDictionary,你可以使用 [dictionary allKeys] 方法来检索一个包含所有键的数组(现在我们称之为 keyArray)。对于 rowCount,您可以返回此 keyArray 中的对象数。要获取需要在单元格中显示的项目,您可以使用 [dictionary objectForKey:[keyArray objectAtIndex:indexPath.row]]] 为显示的单元格获取适当的字典。

    在代码中:

    // use the keyArray as a datasource ...
    NSArray *keyArray = [jsonDictionary allKeys];
    
    // ------------------------- //
    
    // somewhere else in your code ...
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
       return [keyArray count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       static NSString *CellIdentifier = @"Cell";
    
       UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
       if (cell == nil) {
          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
          // set some cell defaults here (mainly design) ...
       }
    
       NSString *key = [keyArray objectAtIndex:indexPath.row];
       NSDictionary *dictionary = [jsonDictionary objectForKey:key];
       // get values from the dictionary and set the values for the displayed cell ...
    
       return cell;
    }
    

    @Tieme: 显然您使用的 URL 已经返回一个数组,您实际上不需要处理字典(您可以只使用数组作为数据源),请查看以下内容:

    SBJSON *json = [[[SBJSON alloc] init] autorelease];
    NSURL *url = [NSURL URLWithString:@"http://www.my-bjoeks.nl/competitions/fetchRoutes/25.json"];
    NSString *string = [[[NSString alloc] initWithContentsOfURL:url] autorelease];
    NSError *jsonError = nil;
    id object = [json objectWithString:string error:&jsonError];
    if (!jsonError) {
       NSLog(@"%@", object);
       NSLog(@"%@", [object class]); // seems an array is returned, NOT a dictionary ...
    }
    
    // if you need a mutableArray for the tableView, you can convert it.
    NSMutableArray *dataArray = [NSMutableArray arrayWithArray:object]
    

    【讨论】:

    • NSArray *keyArray = [jsonDictionary allKeys];给出以下错误:-[__NSArrayM allKeysForObject:]: unrecognized selector sent to instance 0x4e4e660
    • 您确定要在 NSDictionary 对象上调用 -allKeys 方法吗?因为这个方法本质上对任何 NSDictionary 都是可用的,但是您收到的错误表明您正在对另一种类型的对象调用它,请参阅:developer.apple.com/library/mac/#documentation/Cocoa/Reference/…
    • 这不起作用:- (void)viewDidLoad { [super viewDidLoad]; // 从 json 对象加载 NSString *jsonObject = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"<a href="/default/index/tourl?u=aHR0cDovL3d3dy5teS1iam9la3MubmwvY29tcGV0aXRpb25zL2ZldGNoUm91dGVzLzI1Lmpzb24lMjIlNUQlNUQ%3D" rel="nofollow" target="_blank">my-bjoeks.nl/competitions/fetchRoutes/25.json"]]</a>; //为了解析它,我创建了一个 sbjsonparser 对象 NSError *jsonError; NSDictionary *parsedJSON = [[[SBJsonParser new] init] objectWithString:jsonObject error:&amp;jsonError]; // 使用 keyArray 作为数据源 ... NSArray *keyArray = [parsedJSON allKeys]; }
    • @wolfgang-schreurs 谢谢,成功了!
    【解决方案3】:

    eventNameList 应定义为NSMutableArray,而不是NSMutableSetNSMutableArray 响应 -addObject(它将新对象放在数组的末尾)和 -objectAtIndex:,当您考虑它时,表格视图本质上是一个有序列表,数组也是如此,而集合是不是。

    【讨论】:

    • 这看起来很简单,但它对我不起作用,tableview 仍然是空的,当我使用 count 方法计算 numberOfRowsInSection 方法的数据时,我返回 0;
    • @Tieme:在你的 count 方法中放一个断点,检查数组是否为 nil。
    • @Tieme:在您希望它停止的行上放置一个断点(单击编辑器窗口的边缘),然后在调试器中运行程序。
    • 谢谢,刚刚发现。该数组确实为零,我即将找出原因..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    相关资源
    最近更新 更多