BoredToDeath
BoredToDeath

Reputation: 431

display array of dictionaries in UITableView

i am newbie so please dont thrash me thanks... i have the following the array of dictionaries

           NSMutableArray *array;
           NSDictionary *dict,*dict1;

viewdidload method

           array=[NSMutableArray array];
           dict=[NSDictionary dictionaryWithObjectsAndKeys:@"12.00",@"Tomato"];
           [array addObject:dict];

           dict1=[NSDictionary dictionaryWithObjectsAndKeys:@"75.00",@"Cauliflower"];
           [array addObject:dict1];

i want to display the following items in uitableview format

tomato 12.00

Cauliflower 75.00

Upvotes: 1

Views: 1566

Answers (1)

janusfidel
janusfidel

Reputation: 8106

create the data like this.

   array=[NSMutableArray array];
   dict=[NSDictionary dictionaryWithObjectsAndKeys:@"12.00",@"price",@"Tomato",@"name", nil];
   [array addObject:dict];

  dict1=[NSDictionary dictionaryWithObjectsAndKeys:@"75.00",@"price",@"Cauliflower",@"name", nil];
   [array addObject:dict1];

then display on your tableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] ;
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@  %@",[[array objectAtIndex:0]objectForKey:@"name"],[[array objectAtIndex:0]objectForKey:@"price"] ];
  return cell;
}

Upvotes: 3

Related Questions