2011-12-15 6 views
3

私はiOS開発の初心者です。多数の試みの後で;多くのサンプルコードで、私は自分のサーバーからJSON文字列を解析し、その結果を動的なテーブルビューに表示することができました。私の問題は、セルをクリック可能にすることができないため、IDとラベルを別のビューに渡し、別のJSON解析を実行して行の詳細を表示することです。以下は TouchJSON jsonでクリック可能なテーブルを作成するにはどうすればいいですか?

が私のコードです:

#import "jsonviewcontroller.h" 
#import "CJSONDeserializer.h" 
#import "Otel_ItemViewController.h" 

@implementation jsonviewcontroller 


@synthesize tableview; 
@synthesize rows; 

- (void)dealloc { 
    [rows release]; 
    [tableview release]; 
    [super dealloc]; 

} 

// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [rows count]; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell. 
    NSDictionary *dict = [rows objectAtIndex: indexPath.row]; 

    cell.textLabel.text = [dict objectForKey:@"C_NAME"]; 
    cell.detailTextLabel.text = [dict objectForKey:@"CAT_ID"]; 


    return cell; 
} 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSURL *url = [NSURL URLWithString:@"http://zskript.net/categories.php"]; 
    NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; 

    NSLog(jsonreturn); // Look at the console and you can see what the restults are 

    NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding]; 
    NSError *error = nil; 

    // In "real" code you should surround this with try and catch 
    NSDictionary * dict = [[[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error] retain]; 
    if (dict) 
    { 
     rows = [dict objectForKey:@"users"]; 
    } 

    NSLog(@"Array: %@",rows); 


    [jsonreturn release]; 
} 

// Do some customisation of our new view when a table item has been selected 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Make sure we're referring to the correct segue 
    if ([[segue identifier] isEqualToString:@"ShowSelectedMovie"]) { 

     // Get reference to the destination view controller 
     Otel_ItemViewController *vc = [segue destinationViewController]; 

     // get the selected index 
     NSInteger selectedIndex = [[self.tableview indexPathForSelectedRow] row]; 

     // Pass the name and index of our film 
     [vc setSelectedItem:[NSString stringWithFormat:@"%@", [rows objectAtIndex:selectedIndex]]]; 
     [vc setSelectedIndex:selectedIndex]; 
    } 
} 


- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 



@end 

、以下に詳細を表示するビューです:私は、テーブル内のセルをクリックすると、現在

#import "Otel_ItemViewController.h" 

@implementation Otel_ItemViewController 

@synthesize selectedIndex, selectedItem; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [outputLabel setText:selectedItem]; 
    [outputText setText:selectedItem]; 
    [outputImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", selectedIndex]]]; 
} 

@end 

を、私はそれを設定しているが、次のビューに移動するには何も起こりません。誰か助言してくれますか?

ここに更新コードがあります。

マイルートビューコントローラ:

@interface DetailViewController : UIViewController { 


    NSString *CATNAME; 
    NSInteger CATNUMBER; 
    IBOutlet UILabel *labelId; 
    IBOutlet UILabel *LabelName; 

} 

@property (nonatomic) NSInteger CATNUMBER; 
@property (nonatomic, retain) NSString *CATNAME; 

@end 

そしてDetailViewController.m:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSDictionary *dict = [rows objectAtIndex: indexPath.row]; 
    DetailViewController *controller = [[DetailViewController alloc] init]; 
    controller.CATNAME = [dict objectForKey:@"C_NAME"]; 
    controller.CATNUMBER = [dict objectForKey:@"CAT_ID"]; 
    [self.navigationController pushViewController:controller animated:YES]; 
    [controller release]; 
} 

そして、ここではDetailViewController.hであるあなたは、このメソッドを実装する必要が

#import "DetailViewController.h" 

@implementation DetailViewController 

@synthesize CATNAME, CATNUMBER; 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void) viewDidLoad 
{ 
    LabelName.Text = CATNAME; 
    labelId = CATNUMBER; 
// [LabelName setText:CATNAME]; 
// [labelId setText:CATNUMBER]; 
} 
+0

タッチイベントを処理するデリゲートメソッドはどこですか? – Armand

答えて

2

EDIT:私はあなたが(あなたがやってやりたいための最も簡単な方法です)ナビゲーションコントローラを使用していると仮定し、上記のコードでは

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSDictionary *dict = [rows objectAtIndex: indexPath.row]; 
    MyNewViewController *controller = [[MyNewViewController alloc] init]; 
    controller.C_NAME = [dict objectForKey:@"C_NAME"]; 
    controller.CAT_ID = [dict objectForKey:@"CAT_ID"]; 
    [self.navigationController pushViewController:controller animated:YES]; 
    [controller release]; 
} 

新しいビューをロードします。また、あなたが表示したいと思うUIViewControllerから継承するクラスがあると仮定しています。私は私の例でMyNewViewControllerと呼ばれるこのクラスは、それぞれC_NAMEとCAT_IDという2つのメンバーとプロパティを持っていると仮定しています。

- (void) viewDidLoad 
{ 
    labelName.Text = C_NAME; 
    labelId = CAT_ID 
} 

上記の私は正しくありません。しかし、元は同じままです、変数を正しく渡してもうまくいけば、私はblogを見ることができます。それにはまだまだいくつかの作業が必要ですが、素敵な初心者投稿があり、ラベル。上のコードでは、ビューにlabelNameとlabelIdの2つのラベルが含まれていると仮定しています。

そこにはどのセルが選択されたかにアクセスできるので、何が起こる必要があるかを定義できます。

+0

高速応答をありがとう。私はこれをどのように実装するのか分からない。ちょっとしたコードで分かりやすくするのに親切になりますか? –

+0

セルをクリックするとどうなりますか? – Armand

+0

私は、変数として次のビューにjsonからC_NAMEとCAT_IDを渡したいと思います。次のビューでは、ビューのタイトルにC_NAMEを使用し、CAT_IDを使用してデータベースからレコードの詳細を取得する別のクエリを実行します。 –

関連する問題