gpt4 book ai didi

objective-c - 在UITableView中显示字典数组

转载 作者:行者123 更新时间:2023-12-01 19:17:43 25 4
gpt4 key购买 nike

我是新手,请不要打扰我,谢谢...
我有以下字典数组

           NSMutableArray *array;
NSDictionary *dict,*dict1;

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

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

我想以uitableview格式显示以下项目

番茄12.00

花椰菜75.00

最佳答案

像这样创建数据。

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

然后显示在您的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;
}

关于objective-c - 在UITableView中显示字典数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12106849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com