2016-12-09 8 views
1

UIViewControllerの中にUITableViewを設定しようとしています。それは私のためにうまく動作します。私がやったすべてのiOSでUITableViewCellをクリックした後の辞書のキーの値を取得する方法は?

まず、:私はこのようなサーバからの応答を得た

1)

[{"email_id":"[email protected]","id":66,"mobile_no":"1236547895","name":"asad","relation":"dsfs"},{"email_id":"[email protected]","id":67,"mobile_no":"5632145412","name":"raj","relation":"xyz"},{"email_id":"[email protected]","id":68,"mobile_no":"8698819943","name":"suraj","relation":"self"}] 

2)表示に成功したプロトタイプセル内のすべての辞書では4つの値。

すなわちemail_id,mobile_no,nameおよびrelationである。

注:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [menuItems count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"EmergencyContactCell"; 

    EmergencyContactCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    NSDictionary *content = [menuItems objectAtIndex:indexPath.row]; 

    cell.name.text = content[@"name"]; 
    cell.email.text = content[@"email_id"]; 
    cell.mobile.text =content[@"mobile_no"]; 
    cell.relation.text =content[@"relation"]; 

    return cell; 
} 

、それは私のために完璧に動作:はここで私は応じて5つのパラメータを得るが、私はここだけで4

を表示私はそのために何をしたかです。

今、私は何をしたいですか?

私はいずれかのセルをクリックすると、その辞書にある特定のIDを取得したいと思います。 は、私はここで何をすべき:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"%ld",(long)indexPath.row); 

// What should i do here is my question ?? 
    } 

誰もが私の問題を解決することができます。助けが相当になるでしょう。

答えて

2

私は例えば、適切なプロパティを使用し、プレーンなデータ型を必要とするあなたはあなたの中に

NSDictionary *content = [menuItems objectAtIndex:indexPath.row]; 
NSNumber *number = content["id"]; 

のようなものを探していると思う:

int iNumber = number.intValue; 
1

あなたは同じようにあなたのメニューアイテムにアクセスすることができます。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSDictionary *yourItem = [menuItems objectAtIndex:indexPath.row]; 
} 
2
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    NSDictionary *content = [menuItems objectAtIndex:indexPath.row]; 
    NSString *id = content[@"id"]; 
} 

しかし、あなたはcellForRowAtIndexPathに値を解析されたものの、それでもdidSelectRowAtIndexPathでも同様の処理を求める理由を私は理解できないのですか?あなたのdidSelectRowAtIndexPathメソッドにコードの下

2

書き込み:あなたは、リスト内のidを示していないが、あなたがそれにアクセスすることができます

NSDictionary *content = [menuItems objectAtIndex:indexPath.row]; 
NSString *id = content[@"id"]; 

。それはあなたの辞書にあるので。

2

plzはこの

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     int id = [[[menuItems objectAtIndex:indexPath.row]valueForKey:@"id"]intValue]; 
    } 
を使用
関連する問題