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;

       }
4

2 回答 2

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 中:

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

   }

也在 numberOfRowsInSection 中:

   -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   
   {
       return [array count];
   }
于 2013-05-04T07:32:15.277 回答
0

您可以像这样使用较新的语法

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

NSArray *array = @(dict, dict2);
于 2013-05-04T10:23:40.457 回答