带有 NSArray 的 UITableview 包含 NSDictionary 和 NSDictionary 包含另一个 NSDictionary
Posted
技术标签:
【中文标题】带有 NSArray 的 UITableview 包含 NSDictionary 和 NSDictionary 包含另一个 NSDictionary【英文标题】:UITableview with NSArray contains NSDictionary and NSDictionary containg another NSDictionary 【发布时间】:2015-08-08 11:40:41 【问题描述】:这是我得到的服务器端的响应。 匹配是字典。 我正在使用键值,但没有帮助我。
0 =
"shop_name" = sujeet;
subcategory =
0 =
subcategory = "Hair cut";
"subcategory_id" = 16;
;
;
;
1 =
"shop_name" = "Scissors Men's Parlour";
subcategory =
0 =
subcategory = "Children Hair Cut";
"subcategory_id" = 19;
;
;
;
2 =
"shop_name" = "Mirror Beauty Parlour";
subcategory =
0 =
subcategory = "Beauty parlour";
"subcategory_id" = 14;
;
1 =
subcategory = Manicure;
"subcategory_id" = 25;
;
10 =
subcategory = "Wax Half Legs";
"subcategory_id" = 34;
;
11 =
subcategory = "Eyebrow Tint";
"subcategory_id" = 36;
;
2 =
subcategory = Pedicure;
"subcategory_id" = 26;
;
3 =
subcategory = "Facial Massage";
"subcategory_id" = 27;
;
;
;
3 =
"shop_name" = "Sheela's beauty salon";
subcategory =
0 =
subcategory = "Children Hair Cut";
"subcategory_id" = 19;
;
;
;
;
我想将子类别和 shop_name 放在 uitableview 单元格上 我的代码 首先我想知道我得到了多少字典数
myCount = [MAtches count];
NSString *inStr;
for (i = 0; i < myCount; i++)
inStr = [@(i) stringValue];
[connt addObject:inStr];
doing this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
cell.Shop_Name.text=[MAtches valueForKey:[connt objectAtIndex:indexPath.row]];
【问题讨论】:
有谁知道如何打印该值 你的 json 格式很难,因为你在数据中的额外键,比如 0=, 1= 它从 json 中删除的东西不需要它。 @user3189586 有什么东西可以删除那些 0=,1= 我遇到了太多的困难 您的 json 格式错误,请检查。并以这种方式纠正甲酸[ "shop_name" : "sujeet", "subcategory" :[ "subcategory" : "Hair cut", "subcategory_id" : 16, ] , "shop_name": "Scissors Men's Parlour", "subcategory" :[ "subcategory" : "Children Hair Cut", "subcategory_id" : 19, ] ]
【参考方案1】:
因为你有一个不规则的键试试这个,我尝试并使用NSDictionary
:2
来做这个例子.. ;) 干杯。
NSDictionary *tempDic = @
@"shop_name" : @"Mirror Beauty Parlour",
@"subcategory":
@
@0: @
@"subcategory": @"Beauty parlour",
@"subcategory_id": @14
,
@1: @
@"subcategory": @"Manicure",
@"subcategory_id": @25
,
@10:@
@"subcategory": @"Wax Half Legs",
@"subcategory_id": @35
,
@11:@
@"subcategory": @"Eyebrow Tint",
@"subcategory_id": @36
,
@2: @
@"subcategory": @"Pedicure",
@"subcategory_id": @26
,
@3: @
@"subcategory": @"Facial Massage",
@"subcategory_id": @27
;
NSLog(@"shop_name: %@", [tempDic objectForKey:@"shop_name"]);
NSDictionary *tempDict = [tempDic objectForKey:@"subcategory"];
NSArray *allKeys = [tempDict allKeys];
for (int i = 0; i < allKeys.count; i++)
NSString *key = allKeys[i];
NSDictionary *item = [tempDict objectForKey:key];
NSLog(@"key:%@", key);
NSLog(@"subcategory:%@", [item objectForKey:@"subcategory"]);
NSLog(@"subcategory_id:%@", [item objectForKey:@"subcategory_id"]);
NSLog(@"dictionary:%@", item);
【讨论】:
【参考方案2】:尝试使用此代码从您的 json 响应中获取价值
NSMutableDictionary *dict= yourdata store in dictionary;
for (int i=0; i<[dict allKeys].count; i++)
NSLog(@"%@",[dict objectForKey:[NSString stringWithFormat:@"%d",i]]);// return first array object means 0 key value
NSLog(@"%@",[[dict objectForKey:[NSString stringWithFormat:@"%d",i]] objectForKey:@"shop_name"]);// return shop name
在您的表委托方法中使用这种方式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
cell.Shop_Name.text=[[yourJsonDictionary objectForKey:[NSString stringWithFormat:@"%d",indexPath.row]] objectForKey:@"shop_name"]; // print shop name
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [yourJsonDictionary allKeys].count;
【讨论】:
【参考方案3】:如果有人遇到同样的问题
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSArray *key=[MAtches valueForKey:[connt objectAtIndex:indexPath.row]];
cell.Shop_Name.text=[key valueForKey:@"shop_name"];
retrn cell;
【讨论】:
【参考方案4】:处理响应:
- (void)handleResponse:(NSDictionary *)response
responseParsedDictionary = [[NSMutableDictionary alloc] init];
for (id key in response)
NSMutableDictionary *subDictionary = [[NSMutableDictionary alloc] init];
[subDictionary setObject:[[response objectForKey:key] objectForKey:@"shop_name"] forKey:@"shop_name"];
NSDictionary *subcategory = (NSDictionary *)[[response objectForKey:key] objectForKey:@"subcategory"];
NSMutableDictionary *subCategoryArray = [[NSMutableDictionary alloc] init];
for (id subKey in subcategory)
NSMutableDictionary *subcategoryDictionary = [[NSMutableDictionary alloc] init];
[subcategoryDictionary setObject:[[subcategory objectForKey:subKey] objectForKey:@"subcategory"]forKey:@"subcategory"];
[subcategoryDictionary setObject:[[subcategory objectForKey:subKey] objectForKey:@"subcategory_id"]forKey:@"subcategory_id"];
[subCategoryArray setObject:subcategoryDictionary forKey:subKey];
[subDictionary setObject:subCategoryArray forKey:@"subcategory"];
[responseParsedDictionary setObject:subDictionary forKey:key];
`enter code here`
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
cell.Shop_Name.text= [[responseParsedDictionary objectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]] objectForKey:@"shop_name"];
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return responseParsedDictionary.count;
【讨论】:
以上是关于带有 NSArray 的 UITableview 包含 NSDictionary 和 NSDictionary 包含另一个 NSDictionary的主要内容,如果未能解决你的问题,请参考以下文章
根据单元格选择,使用 NSArray 中的选择对象重新加载 UITableView
带有一个可见单元格的 UITableView:确定哪个最可见