带有JSON帮助的UITableView

我正在尝试将一些JSON数据输入我的iPhone应用程序,数据正常,因为我有NSLog告诉我的。

我遇到的问题是尝试将结果显示在UITableView中。 我在导航栏控制器下面有一个导航控制器,导航控制器包含一个表视图控制器,它加载另一个NIB文件,其表视图连接到一个类,该类是委托和数据源委托。

我还需要将结果分为几个部分 – 这些部分

  • 英国
  • 苏格兰
  • 威尔士
  • N.Ireland

要了解我正在使用的JSON字符串, 请参阅此字符串。

正如你所看到的那样JSON不适合这些部分,但我还没有实现这个,所以我需要事先知道,所以我不需要稍后修改很多代码。

好的 – 我正在使用Stig JSON解析器。

这是我的ListVenuesView.h(连接到表视图)

#import  #import "SBJson.h" @interface ListVenuesView : UITableViewController  { IBOutlet UITableView *venueList; NSMutableDictionary *jsonArray; } @property (nonatomic, retain) IBOutlet UITableView *venueList; @property (nonatomic, retain) NSMutableDictionary *jsonArray; @end 

jsonArray用于存储JSON数据并最终存储正确的数组。

这是我的ListVenuesView.m(有问题的关键区域)

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

另外,我如何使用单元格中的数据转到导航控制器的另一个子视图,该子视图为我提供了一个后退按钮,并显示来自JSON字符串的信息,仅用于已被挖掘的特定单元格。

我认为这与它有关吗? 不确定,因为这是我正在构建的第一个应用程序! 所以可能期待更多的求助 – 哈! ;)

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

在选择行时,如u所述,我们将导航到另一个视图。 我们假设视图控制器是DetailViewController,它是UIViewController的子类。

在DetailViewController.h中,声明一个NSDictionary对象。

在DetailViewController.m中,添加

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

在ParentViewController中,你的didSelectRow ..方法应该是这样的。

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

在第二个视图控制器中,你可以用_venueDict做任何你想做的事情。

弥敦道,

因为您希望重用通过多个​​ViewController从JSON提要解析的数据,所以最好的方法是构建一个对象模型,以便您可以将列表中选定行的对象传递给详细的ViewController。

我还将JSON解析代码分离到一个单独的类中,而不是将它保存在ViewController中。 您可以在此链接上找到要获取JSON的类。

自定义代码解析JSON提要的结果将返回一个NSDictionary,其中包含您提到的节名称作为键。 NSDictionary中这些键的值将是一个自定义对象数组,其中包含一行(和详细信息屏幕)的所有相关数据。

希望这能帮到你的路。

jsonArray是NSMutableDictionary。

必须使用

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

这可能有所帮助。