2011-07-07 38 views
0

我想提供一些JSON數據到我的iPhone應用程序,數據進來罰款,因爲我有NSLog告訴我這樣。UITableView與JSON的幫助

我遇到的問題是試圖讓結果顯示在UITableView中。我有一個導航控制器在標籤欄控制器下,導航控制器包含一個表視圖控制器,該控制器加載另一個NIB文件,其中一個表視圖連接到一個類,該類是委託和數據源委託。

我還需要將結果歸類爲多個部分 - 這些是

  • 英格蘭
  • 蘇格蘭
  • 威爾士
  • N.Ireland

得到什麼JSON的想法字符串我正在使用see this one

正如你所看到的JSON不適合這些部分,但我還沒有實現這一點,所以我需要事先知道,所以我不必稍後修改很多代碼。

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

這是我的ListVenuesView.h(連接到表視圖)

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

而且我怎麼可以使用數據在細胞中去導航控制器的另一子視圖,給了我一個返回按鈕和僅顯示已被點擊的特定單元格的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]; 
    */ 
} 

回答

3

在選擇一行時,如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。

+0

非常感謝這個Gomathi! –

0

jsonArray是NSMutableDictionary。

必須使用

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

這可能會有幫助。

+0

對不起Dileep - 我不明白你的意思? 我得到'使用不明確的標識符'錯誤' –

+0

jsonArray用於存儲JSON數據並最終存儲正確的數組。但你聲明爲NSMutableDictionary * jsonArray; –

+0

好的 - 那麼我現在怎麼投jsonArray? –

2

彌敦道,

,因爲你想重用超過一個視圖控制器處理這個是要建立一個對象模型,這樣就可以通過對象選定行的最佳方式從JSON提要解析數據在列表中的細節ViewController。

我也將JSON解析代碼分離成一個單獨的類,而不是保留在ViewController中。 你可以在這個link上找到獲取JSON的類。

解析JSON訂閱源的自定義代碼的結果將爲您提到的部分名稱返回一個NSDictionary,並將其作爲關鍵字。 NSDictionary中這些鍵的值將是一個包含一行(和詳細信息屏幕)所有相關數據的自定義對象的數組。

希望這可以幫助你的方式。