2012-05-02 22 views
0

我的問題是,當單元格被點擊時,是否可以獲取文本數據並從php的ViewController中讀取它?UITableView - 點擊時獲取數據

我猜didSelectRowAtIndexPath是做到這一點的關鍵,但我不知道如何寫。

我可以從php獲取數據到我的TableViewController,如下所示;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [json count]; 

} 

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

    NSDictionary *info = [json objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [info objectForKey:@"Artist"]; 
    cell.detailTextLabel.text = [info objectForKey:@"Song"]; 


    return cell; 
} 
+0

是的,它是可能的用途做了選擇方法,並在方法編寫retrive數據的代碼,只有陣列之前,如果你想在刷新你的tableview或撥打新的視圖控制器,並通過它在那裏。 – vishiphone

回答

0

您的代碼是我認爲是錯誤的。檢查json中是否有任何數據通過檢查

NSLog(@"%@ and %@",[info objectForKey:@"Artist"], [info objectForKey:@"Song"]); 

並告訴我是否有任何結果出來。如果沒有,那麼你的PHP中有一些錯誤,你的數據應該顯示在那裏。

0

您寫的代碼與您在開始解釋的內容相矛盾。但是,這是應該如何完成:

首先你需要獲取你想要顯示的數據。你可以從外部網站(php,如你所說),plist,xml或任何你想要的。你可以在啓動你的tableview的時候(例如使用ASIHTTPRequest)或者在啓動你的tableview之前完成這個工作,然後使用自定義的init方法發送它。 其次,你需要閱讀你收到的內容。通常很高興能夠獲得NSArray格式。這取決於您從中獲取數據的數據源的實現。

如果你是新手Objective-C和可可觸摸,你應該從一些關於UITableView的快速教程開始。我自己寫了一篇,但是現在我猜它已經過時了。你仍然可以看看它:http://paulpeelen.com/2009/08/14/developing-with-iphone-3-0-x-simple-uitableview/

0

使用此方法的代碼。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //retrive your data here. 
}