2013-05-04 4 views
0

셀 배열 대신 NSDictionary을 사용하고 싶습니다.사전 설정을 사용하여 셀의 제목을 설정하는 방법

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

     UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
     //Add the Bg Image to the cell 
     //Add the Label 
     UILabel *cellTitle=[[UILabel alloc]initWithFrame:CGRectMake(15, 7, 300, 30)]; 
     [cellTitle setBackgroundColor:[UIColor clearColor]]; 
     [cellTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]]; 
     [cellTitle setTextColor:[UIColor darkGrayColor]]; 
     [cellTitle setText:[[cellArray objectAtIndex:indexPath.section]    objectAtIndex:indexPath.row]]; 
     [cell.contentView addSubview:cellTitle]; 
     return cell; 

     } 

답변

0
NSDictionary *dictionary1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"ABC",@"Name",@"12",@"Age", nil]; 

NSDictionary *dictionary2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"DEF",@"Name",@"14",@"Age", nil]; 

NSDictionary *dictionary3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"GHI",@"Name",@"16",@"Age", nil]; 

NSDictionary *dictionary4 = [[NSDictionary alloc] initWithObjectsAndKeys:@"JKL",@"Name",@"18",@"Age", nil]; 

NSArray *array = [[NSArray alloc] initWithObjects:dictionary1,dictionary2,dictionary3,dictionary4, nil]; 

이제 cellForRowAtIndexPath에서 : 다음 numberOfRowsInSection에도

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

    UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    //Add the Bg Image to the cell 
    //Add the Label 
    UILabel *cellTitle=[[UILabel alloc]initWithFrame:CGRectMake(15, 7, 300, 30)]; 
    [cellTitle setBackgroundColor:[UIColor clearColor]]; 
    [cellTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]]; 
    [cellTitle setTextColor:[UIColor darkGrayColor]]; 
    [cellTitle setText:[[array objectAtIndexPath:indexPath.row] objectForKey:@"Name"]]; 
    [cell.contentView addSubview:cellTitle]; 
    return cell; 

    } 

:이 같은 새로운 구문을 사용할 수 있습니다

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

다음

내 코드입니다

NSDictionary *dict = @{@"key" : @"value", @"key2" : @"value2"}; 
NSDictionary *dict2 = @{@"key" : @"value", @"key2" : @"value2"}; 

NSArray *array = @(dict, dict2); 
관련 문제