开发者

UITableView with JSON help

I am trying to feed in some JSON data to my iPhone app, the data is coming in fine as I have NSLog's telling me so.

The problem I am having is trying to get the results to show in a UITableView. I have a navigation controller underneath a tab bar controller, the navigation controller contains a table view controller which loads another NIB file with a table view connected to a class which is the delegate and data source delegate.

I also need to categorize the results into sections - these being

  • England
  • Scotland
  • Wales
  • N.Ireland

To get an idea of what JSON string I am using see this one.

As you can see the JSON does not cater for the sections but I am yet to implement this, so i would need to know beforehand so I do not have to amend much code later on.

OK - I am using Stig JSON parser.

Here is my开发者_如何学Go ListVenuesView.h (connected to table view)

#import <UIKit/UIKit.h>
#import "SBJson.h"

@interface ListVenuesView : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
    IBOutlet UITableView *venueList;
    NSMutableDictionary *jsonArray;
}

@property (nonatomic, retain) IBOutlet UITableView *venueList;
@property (nonatomic, retain) NSMutableDictionary *jsonArray;

@end

jsonArray is used to store the JSON data and eventually the proper array.

And here is my ListVenuesView.m (key areas in question)

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"Table View Loaded");

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    // This is where we load the JSON data

    NSURL *jsonURL = [NSURL URLWithString:@"http://www.thebigfishexperience.org.uk/sources/ajax/venue-json.php?location=peterborough"];

    NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];
    NSLog(@"%@", jsonData);

    // Convert jsonData to array
    self.jsonArray = [jsonData JSONValue];
    NSLog(@"%@", jsonArray);

    NSLog(@"count is: %i", [self.jsonArray count]);

    // Release NSString and NSURL
    [jsonURL release];
    [jsonData release];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.jsonArray 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] autorelease];
    }

    NSMutableDictionary *dict = [self.jsonArray objectAtIndex: indexPath.row];

    cell.textLabel.font = [UIFont fontWithName:@"Arial" size:15.0];

    cell.textLabel.text = [dict objectForKey:@"venueName"];
    cell.detailTextLabel.text = [dict objectForKey:@"venueCordDist"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    // Configure the cell...

    return cell;

Also how can I use the data in the cells to go to another subview of the nav controller which gives me a back button and displays the info from the JSON string just for that particular cell that has been tapped.

I think this has something to do with it? Not sure though as this is my first app i am building! So probably expect more pleas of assistance - ha ! ;)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"Nib name" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
}


On selecting a row, as mentioned by u, we are navigating to another view. Let us assume that the view controller is DetailViewController which is a sub-class of UIViewController.

In the DetailViewController.h , declare a NSDictionary object.

In DetailViewController.m, add

-(void)setVenueDict:(NSDictionary*)venueDict
{
    if( _venueDict )
    {
      [_venueDict release];
    }
    _venueDict = [venueDict retain];

}

In ParentViewController, ur didSelectRow.. method should be like this.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.

     DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"Nib name" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     NSDictionary *dict = [self.jsonArray objectAtIndex: indexPath.row];
     [detailViewController setVenueDict:dict];
     detailViewController.title = [dict objectForKey:@"venueName"];
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];

}

In the second view controller, u can do whatever u want with the _venueDict.


Nathan,

since you want to reuse the data parsed from the JSON feed over more than one ViewController the best way to approach this is to build an object Model so that you can pass the object for the selected row in the list to the detail ViewController.

I would also separate the JSON parsing code into a separate class and not keep it in the ViewController. You can find classes to fetch JSON on this link.

The result from the custom code to parse the JSON feed would give back a NSDictionary with as keys the section names you mention. And the value in the NSDictionary for those keys would be an array of your custom objects that contain all the relevant data for one row (and detail screen).

Hope this helps you on your way.


jsonArray is NSMutableDictionary.

have to use

[jsonArray objectForKey:key];
//check this line 
NSMutableDictionary *dict = [self.jsonArray objectAtIndex: indexPath.row];

this may help.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜