我正在尝试向我的 iPhone 应用程序提供一些 JSON 数据,数据正常,NSLog 告诉我如此。
我遇到的问题是试图让结果显示在 UITableView 中。我在选项卡栏 Controller 下面有一个导航 Controller ,导航 Controller 包含一个 TableView Controller ,它加载另一个 NIB 文件,其中 TableView 连接到一个类,该类是委托(delegate)和数据源委托(delegate)。
我还需要将结果分类为部分 - 这些是
- 英格兰
- 苏格兰
- 威尔士
- 北爱尔兰
了解我使用的是什么 JSON 字符串 see this one .
如您所见,JSON 不适合这些部分,但我还没有实现它,所以我需要事先知道,这样我以后就不必修改太多代码。
好的 - 我正在使用 Stig JSON 解析器。
这是我的 ListVenuesView.h(连接到 TableView )
#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 数据并最终存储正确的数组。
这是我的 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;
另外,我如何使用单元格中的数据转到导航 Controller 的另一个 subview ,该 View 为我提供后退按钮并显示来自 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 所述,选择一行后,我们将导航到另一个 View 。让我们假设 View Controller 是 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];
}
在第二个 View Controller 中,你可以用 _venueDict 做任何你想做的事。
关于iphone - 带有 JSON 帮助的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6609252/