如何将JSON数据放入表格视图?请帮助我,我生活在噩梦中:) [英] How do I put this JSON data into my table view? Please help me, I'm living in a nightmare :)

查看:125
本文介绍了如何将JSON数据放入表格视图?请帮助我,我生活在噩梦中:)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对将JSON数据放入表格视图有疑问.我现在已经成功解析了数据,因为它已经出现在控制台中了,但是我终生无法弄清楚如何将这些数据放入表格视图中.我花了整整一周的时间努力使它在阳光下尝试所有事物的组合.

I have a question with respect to putting JSON data into a table view. I'm at the point where I have successfully parsed the data, as its coming up in my console - but I cant for the life of me figure out how to put this data into my table view. I've spent a whole week solid trying to get this to work trying every combination of things under the sun.

在大多数情况下,它只是崩溃了,有时它会告诉我"[NSCFString count]:无法识别的选择器",我相信这是因为我正在尝试计算引发异常的字符串吗? -无论如何,使用下面的代码,它只会在控制台中打印我想要的数据,并在表格视图中向我显示大量的灰线,因此它不会崩溃(这是我所猜测的最好的选择).

Most of the time it just crashes, sometimes it tells me "[NSCFString count]: unrecognized selector" which I believe happens because I'm trying to count a string which throws an exception? - Anyways, with the code below, it just prints the data I want in the console and shows me a load of grey lines in the table view, so its not crashing (which is the best I can do at the moment I guess).

任何有关如何使用所有eventName填充表的帮助/建议将不胜感激.我此视图最终计划是具有其中当选择推显示该特定事件的数据的新的视图控制器列表中eventNames.

Any help/advice on how to populate the table with all of the eventNames will be greatly appreciated. My eventual plan for this view was to have the eventNames in the list which when selected push a new view controller showing data for that specific event.

这是视图加载方法:

- (void)viewDidLoad {
[super viewDidLoad];

NSURL *url = [NSURL URLWithString:@"http://myurl........."];
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url];

NSLog(jsonreturn); //successfully returns the result of the page

    //to parse it i have made a sbjsonparser object
    SBJsonParser *json = [[SBJsonParser new] autorelease];
    NSError *jsonError;
    NSDictionary *parsedJSON = [json objectWithString:jsonreturn error:&jsonError];

        //if successful, i can have a look inside parsedJSON - its worked as an NSdictionary and NSArray
        NSArray* events = [parsedJSON objectForKey:@"Events"];
        //eventNameList = [parsedJSON objectForKey:@"Events"];
        NSLog(@"show me events: %@", events);
        //NSLog(@"show me events: %@", eventNameList);

            //lets try and get to rows
            //NSEnumerator *enumerator = [events objectEnumerator];
            NSEnumerator *enumerator = [events objectEnumerator];
            NSDictionary* item;
                while (item = (NSDictionary*)[enumerator nextObject]) {
            NSLog(@"event item:eventName = %@", [item objectForKey:@"EventName"]); //everything to this point works and shows in the console

            }

}

这是表格视图代码:

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

//customise number of rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [eventNameList count];

NSLog(@"here");

}

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

static NSString *CellIdentifier = @"Cell";

//try to get a reusable cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//create a new cell if there is not reusable cell available
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

//set the text display for the cell
NSString *cellValue = [eventNameList objectAtIndex:indexPath.row];

cell.textLabel.text = cellValue;

//NSLog(@"event item from table view:eventName = %@", eventNameList);

return cell;
}

在此先感谢您的帮助或指点:)

Thanks in advance, any help or pointers are greatly welcomed and appreciated :)

推荐答案

如果我是你,我将创建一个自定义对象:

If I were you, I'd create a custom object:

NSEnumerator *enumerator = [events objectEnumerator];
NSDictionary* item;

while (item = (NSDictionary*)[enumerator nextObject]) {
  Event *event = [[Event alloc] init];
  event.eventName = [item objectForKey:@"EventName"];
  // set more properties here
  [eventNameList addObject:event];
  [event release];
}

然后,您可以在cellForRowAtIndexPath:

Event *event = (Event *)[eventNameList objectAtIndex:indexPath.row];

cell.textLabel.text = event.eventName;

这篇关于如何将JSON数据放入表格视图?请帮助我,我生活在噩梦中:)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆