2012-10-26 2 views
0

이름을 나열 할 응용 프로그램을 만들었습니다. 이름이 도청 될 때마다 특정 이름의 나이를 반환하고 싶습니다. 저는 이름과 나이를 동적으로 추가하고 있습니다. 키 - 값 쌍을 사용하면 추측 된 값을 쉽게 검색 할 수 있습니다. 누구나 KeyValue (NSDictionary) 쌍을 사용하는 방법을 알려 줄 수 있습니까?UITableView에 대한 키 값 쌍 사용

답변

1
NSDictionary *nameAgePair=[[NSDictionary alloc]init]; 
[nameAgePair setValue:@"30" forKey:@"Name1"]; 
[nameAgePair setValue:@"25" forKey:@"Name2"]; 

NSArray *arr=[nameAgePair allKeys]; 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITableViewCell *cell = [[tableView dequeueReusableCellWithIdentifier:@"cell"] autorelease]; 

    UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)]; 
    [lbl setText:[arr objectAtIndex:indexPath.row]]; 
    [cell.contentView addSubview:lbl]; 

} 

이와 같이 선택한 값에 액세스 할 수 있습니다.

[nameAgePair objectForKey:@"selected row"]; 

참고 : 키 - 값 코딩을 사용하는 경우 키 (이름)가 고유해야합니다.