开发者

How do I put this JSON data into my table view? Please help me, I'm living in a nightmare :)

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.

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).

Any help/advice 开发者_C百科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.

Here's the view did load method:

- (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

            }

}

And here is the table view code:

//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];
}

Then you can use the following in your cellForRowAtIndexPath:

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

cell.textLabel.text = event.eventName;


Well, you've probably solved this by now, but you don't initialize eventNameList in the first block of code, you initialize another variable called events.

Depending on what eventNameList actually is, you could get an unrecognized selector for count.

It seems the tableView code should work if you change

return [eventNameList count];

to

return [events count];

and

[eventNameList objectAtIndex:indexPath.row];

to

[events objectAtIndex:indexPath.row];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜