带有JSON帮助的UITableView [英] UITableView with JSON help
问题描述
我正在尝试将一些JSON数据提供给我的iPhone应用程序,由于NSLog告诉我,数据可以正常使用.
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.
我遇到的问题是试图使结果显示在UITableView中.我在标签栏控制器下面有一个导航控制器,该导航控制器包含一个表视图控制器,该控制器加载另一个NIB文件,并将另一个表视图连接到一个类,该类是委托和数据源委托.
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
- 英国
- 苏格兰
- 威尔士
- 爱尔兰北部
要了解我使用的是什么JSON字符串,请请参阅此.
To get an idea of what JSON string I am using see this one.
您可以看到JSON不能满足各节的要求,但是我还没有实现,所以我需要事先知道,这样以后就不必修改太多代码了.
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.
确定-我正在使用Stig JSON解析器.
这是我的ListVenuesView.h(已连接到表格视图)
Here is my 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用于存储JSON数据,并最终存储适当的数组.
jsonArray is used to store the JSON data and eventually the proper array.
这是我的ListVenuesView.m(有问题的关键区域)
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;
此外,我该如何使用单元格中的数据转到导航控制器的另一个子视图,该子视图为我提供了一个后退按钮,并显示JSON字符串中仅针对已点击的特定单元格的信息.
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];
*/
}
推荐答案
如u所提到的,在选择一行时,我们将导航到另一个视图.让我们假设视图控制器是DetailViewController,它是UIViewController的子类.
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.
在DetailViewController.h中,声明一个NSDictionary对象.
In the DetailViewController.h , declare a NSDictionary object.
在DetailViewController.m中,添加
In DetailViewController.m, add
-(void)setVenueDict:(NSDictionary*)venueDict
{
if( _venueDict )
{
[_venueDict release];
}
_venueDict = [venueDict retain];
}
在ParentViewController中,您的didSelectRow ..方法应如下所示.
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];
}
在第二个视图控制器中,您可以使用_venueDict做任何您想做的事情.
In the second view controller, u can do whatever u want with the _venueDict.
这篇关于带有JSON帮助的UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!