2012-08-02 45 views
0

我能夠得到的細胞像這樣的文本值:ios - 如何從UITableView單元格獲得id完整數組值?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger *row = [indexPath row]; 

    // NOW GET THE NAME AND ID FROM THAT ROW 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSString *cellText = cell.textLabel.text;  

    NSLog(cellText);  
} 

,但我需要能夠獲得該項目的ID,或至少使用該行從數組獲得該項目。

這是我如何得到填充的UITableView的數組:

@synthesize items_array; 

及更高版本:

    items_array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 

       [self.itemList reloadData]; 

和陣列具有JSON這對於每一個項目,像ITEM_ID領域,ITEM_NAME

因此,當用戶點擊列表中的項目時,如何獲取id字段?

謝謝!

+0

您是否有自定義單元格?它是否有item_id屬性?我就是這麼做的。或者你將不得不搜索(2d?)數組與索引path.row作爲索引 – mkral 2012-08-02 14:55:06

+0

@mkral啊我看到了,所以基本上,我必須把一個自定義對象放入單元格中,對嗎?只需在uitableview中顯示對象中的一個字段?出於好奇,你能否提供一些示例代碼來說明你如何做到這一點。 – GeekedOut 2012-08-02 15:30:55

+0

如果你想訪問JSON數據,那麼runmad的答案將會起作用。你仍然需要使用它來設置'cell.fieldID'你如何創建單元格?是基於一些返回的JSON值的文本? – mkral 2012-08-02 15:36:30

回答

1
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSDictionary *itemInfo = [items_array objectAtIndex:indexPath.row]; 
    NSString *itemID = [itemInfo objectForKey:@"item_id"]; 
} 
3
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *idField = [items_array objectAtIndex:indexPath.row]; 
} 
+0

我想你的意思是'NSDictionary *信息= [數據objectAtIndex:indexPath.row];'然後'NSString * idField = [數據objectForKey:@「item_id」'根據JSON的結構。 – mkral 2012-08-02 15:01:21

+0

@runmad我得到一個錯誤,試圖做到這一點NSLog(idField);在你建議的行後面:「無法識別的選擇器發送到實例」 – GeekedOut 2012-08-02 15:39:14

+0

嘗試'NSLog(@「Class:%@」,[[items_array objectAtIndex:indexPath.row] class]'我認爲它是字典的類型,但我可能是錯的 – mkral 2012-08-02 15:42:29

相關問題