【问题标题】:Parsing JSON data to list on a UItableview for iPhone解析 JSON 数据以在 iPhone 的 UItableview 上列出
【发布时间】:2011-11-08 21:05:34
【问题描述】:

我确实尝试将 JSON 数据带到 iPhone 并使用 NSLOG 连接打印传入的 JSON 数据。能够打印和测试特定节点。我开始使用 for-loop 循环遍历提要和显示数组它在 UItableview 中并且无法这样做。尝试了许多选项,但无法解决这个问题。请查看代码,让我知道我在哪里以及做错了什么。我仍在学习 iPhone 编码,因此任何输入输出都会非常有帮助。请找到代码下面。

#import "RootViewController.h"
#import "SBJson.h"
#import "JSON.h"

@implementation RootViewController

NSMutableArray *listOfStates;
NSArray *exercises;
NSMutableData *responseData;


- (void)viewDidLoad
{
    //---initialize the array---
    listOfStates = [[NSMutableArray alloc] init];


    responseData = [[NSMutableData data] retain];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost/~xxxx/MyWorks/index.php?playerid=0"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self ];
    //NSURL *url = [NSURL URLWithString:@""]; 

    [super viewDidLoad];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"didReceiveResponse"); [responseData setLength:0]; }
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data]; }

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Connection failed: %@", [error description]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];

    NSDictionary *dictionary = [responseString JSONValue];
    NSArray *response = [dictionary valueForKey:@"feed"];

    NSLog(@"Here is the title of the feed: %@", [response valueForKey:@"video"]);


    exercises = [[NSArray alloc] initWithArray:response];

    [[self tableView] reloadData];

}




/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [exercises count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];

    NSDictionary *dictionary = [responseString JSONValue];
    NSArray *response = [dictionary valueForKey:@"feed"];

    //create a cell
    UITableViewCell *cell = [[UITableViewCell alloc]
                             initWithStyle:UITableViewCellStyleDefault
                             reuseIdentifier:@"cell"];



    // fill it with contnets

    int ndx;
    for (ndx = 0; ndx <dictionary.count; ndx++) {
        NSDictionary *player = (NSDictionary *)[dictionary objectAtIndex:ndx];
        NSLog(@"Player: %@", [player valueForKey:@"player"]); 
    }
    NSString *cellValue = [player objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;
    // return it
    return cell;

}

【问题讨论】:

  • 你能再具体一点吗?究竟是什么不工作?另外,您收到的 JSON 是什么样的?
  • @Tim 当我尝试将 JSON 数据的值发布到 cellForRowAtIndexPath 函数中的 cellValue 中时,它失败了。请在此处找到 JOSN 数据 {"feed": [{"id":"1","player":"player 1","video":"big_hit.mp3"},{"id":"1", "player":"player 2","video":"tribute .mp3"},{"id":"2","player":"player 3","video":"Interview.mp3"}]}

标签: iphone json uitableview xcode4


【解决方案1】:

在不知道发生了什么的情况下很难说出哪里出了问题,但这里有几个观察结果。

  • 您似乎多次解析您的响应。一次在 connectionDidFinishLoading 中创建“练习”数组,然后每次通过 tableView 呈现单元格时再次:cellForRowAtIndexPath:。我假设您在第一次从 JSON 数据创建练习数组时获得了所需的所有数据。在这种情况下,tableView:cellForRowAtIndexPath: 应该只需要从练习数组中取出对象,而不是处理响应数据。
  • 同样在这里,我无法弄清楚您要对使用“播放器”的循环执行什么操作 - 似乎您正在记录信息,但没有对这些播放器对象执行任何操作。除了不使用它们之外,在我看来,这些未释放的对象还会导致内存泄漏。

根据您在评论中添加的 JSON,练习数组似乎将包含 NSDictionary 对象。这意味着您设置单元格文本的代码不正确。而不是

NSString *cellValue = [response objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

你可能想要类似的东西

NSDictionary *exercise = [response objectAtIndex:indexPath.row];
cell.textLabel.text = [exercise valueForKey"player"];

【讨论】:

  • 我是 iPhone 编码的新手。我无法在 cellForRowAtIndexPath 使用“练习”数组。于是又重新解析了一遍。实际上,循环是打印并检查我能够做到的播放器对象。但无法在tableview上显示。我已经编辑了代码..“播放器”循环
  • 您不想在这里再次解析代码:这意味着您将解析表格中的每一行,并且因为您没有缓存单元格,所以每次行返回时都会再次解析滚动时进入可见范围。正确设置练习数组,然后在创建表格单元格时使用它。
  • 感谢您的回复。我认为我太困惑了,需要正确掌握基础知识。我按照您的建议做了并删除了不需要的解析,并将练习对象用于单元格视图显示并且它有效.再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
  • 2010-12-17
  • 1970-01-01
  • 2016-02-20
相关资源
最近更新 更多