根据字典数组为 tableview 设置 numberOfRowsInSection
Posted
技术标签:
【中文标题】根据字典数组为 tableview 设置 numberOfRowsInSection【英文标题】:Set numberOfRowsInSection for tableview as per the Array of Dictionaries 【发布时间】:2018-07-04 14:30:19 【问题描述】:我有一个小问题...
这里有一个名为 arrayDishDetails 的数组
"dishid": "5",
"cusineid": "1",
"dishname": "Cheese Fries",
"dishimage": "noimage.png",
"type": "NA",
"description": "VEG FRIES",
"price": "120",
"swiggy_status": "enable",
"preflag": "true",
"pre": [
"preid": "112",
"pretype": "Multi",
"prenm": "BATTER FRIED FISH",
"preprice": "70",
"cat_id": "3"
,
"preid": "109",
"pretype": "Multi",
"prenm": "JALAPENO POPPERS",
"preprice": "50",
"cat_id": "3"
,
"preid": "110",
"pretype": "Multi",
"prenm": "CHICKEN SAUSAGES",
"preprice": "50",
"cat_id": "3"
,
"preid": "111",
"pretype": "Multi",
"prenm": "CHICKEN NUGGETS",
"preprice": "50",
"cat_id": "3"
,
"preid": "113",
"pretype": "Multi",
"prenm": "SHREDDED CHICKEN",
"preprice": "50",
"cat_id": "3"
,
"preid": "114",
"pretype": "Multi",
"prenm": "CHICKEN POPCORN",
"preprice": "50",
"cat_id": "3"
,
"preid": "105",
"pretype": "Multi",
"prenm": "GRATED CHEESE",
"preprice": "30",
"cat_id": "3"
,
"preid": "272",
"pretype": "Multi",
"prenm": "Chili",
"preprice": "20",
"cat_id": "6"
,
"preid": "273",
"pretype": "Multi",
"prenm": "Jalapeno Cheese",
"preprice": "20",
"cat_id": "6"
,
"preid": "274",
"pretype": "Multi",
"prenm": "Salsa",
"preprice": "20",
"cat_id": "6"
,
"preid": "275",
"pretype": "Multi",
"prenm": "BBQ",
"preprice": "20",
"cat_id": "6"
,
"preid": "276",
"pretype": "Multi",
"prenm": "Mayo",
"preprice": "20",
"cat_id": "6"
,
"preid": "277",
"pretype": "Multi",
"prenm": "Schezwan",
"preprice": "20",
"cat_id": "6"
,
"preid": "278",
"pretype": "Multi",
"prenm": "Pizza Sauce",
"preprice": "20",
"cat_id": "6"
,
"preid": "279",
"pretype": "Multi",
"prenm": "Chipotle",
"preprice": "20",
"cat_id": "6"
,
"preid": "280",
"pretype": "Multi",
"prenm": "Tandoori",
"preprice": "20",
"cat_id": "6"
,
"preid": "281",
"pretype": "Multi",
"prenm": "Garlic Mayo",
"preprice": "20",
"cat_id": "6"
,
"preid": "282",
"pretype": "Multi",
"prenm": "Tartare sauce",
"preprice": "20",
"cat_id": "6"
,
"preid": "283",
"pretype": "Multi",
"prenm": "Thai Sweet Chili",
"preprice": "20",
"cat_id": "6"
,
"preid": "284",
"pretype": "Multi",
"prenm": "Makhani Gravy",
"preprice": "20",
"cat_id": "6"
,
"preid": "106",
"pretype": "Multi",
"prenm": "CRUSHED TORTILLAS",
"preprice": "20",
"cat_id": "3"
,
"preid": "107",
"pretype": "Multi",
"prenm": "BEANS",
"preprice": "20",
"cat_id": "3"
,
"preid": "115",
"pretype": "Multi",
"prenm": "FRIED MAGGI",
"preprice": "20",
"cat_id": "3"
,
"preid": "270",
"pretype": "Multi",
"prenm": "Chat Masala",
"preprice": "10",
"cat_id": "3"
,
"preid": "108",
"pretype": "Multi",
"prenm": " MAGIC MASALA",
"preprice": "10",
"cat_id": "3"
],
"modifier_cat": [
"cat_id": "3",
"cat_name": "ADD ONS",
"modifier_selection": "1"
,
"cat_id": "6",
"cat_name": "Extra Sauces",
"modifier_selection": "1"
]
现在在我的表格视图中,我像这样设置 titleForHeaderInSection。
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
return [[[self.arrayDishDetails valueForKey:@"modifier_cat"] valueForKey:@"cat_name"] objectAtIndex:section];
我这样设置 numberOfSectionsInTableView..
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return [[self.arrayDishDetails valueForKey:@"modifier_cat"] count];
现在,我的问题是,如何过滤 "pre" 数组以通过 "cat_id" 从 "prenm" 从 "modifier_cat" 数组,这样我就可以得到 2 个不同的类别?
例如:-
for "cat_id" = "3" 我想要所有的 "prenm" 像 "prenm": "BATTER FRIED FISH","JALAPENO POPPERS", "CHICKEN "pre" 数组中的 SAUSAGES" 等。
"cat_id" = "6"
也一样我已经为过滤器数组寻找了许多解决方案,但无法获得我的帮助。 任何帮助将非常感激。谢谢!
编辑 1:
我从@Karthik 的回答中得到了一些提示,我为我的表格视图设置了行数,如下所示:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"cat_id = %@", [[[self.arrayDishDetails valueForKey:@"modifier_cat"]valueForKey:@"cat_id"]objectAtIndex:section]];
NSArray *filteredArray = [[self.arrayDishDetails valueForKey:@"pre"] filteredArrayUsingPredicate:predicate];
return filteredArray.count;
现在,如何根据 "modifier_cat" 的 "cat_id" 为每个部分(此处为 0 和 1 两个部分)设置 "prenm" 数组?
【问题讨论】:
【参考方案1】:我认为您正在寻找使用自定义对象过滤数组。我假设您放置的 JSON 已映射到自定义对象。添加以下代码以从 modifier_cat 数组中过滤带有 cat_id 的 pre 数组。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [self getFilteredArray: section].count;
要显示包含项目的单元格,请使用以下方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @"FishDetailsCell"];
UILabel *fishStyle = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, tableView.frame.size.width, 100)];
Pre *pre = [[self getFilteredArray:indexPath.section] objectAtIndex: indexPath.row];
fishStyle.text = pre.prenm;
fishStyle.textAlignment = NSTextAlignmentCenter;
[cell addSubview: fishStyle];
return cell;
// 过滤数组的复用方法
- (NSArray *)getFilteredArray:(NSInteger)section
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"cat_id = %@", [[[self.arrayDishDetails valueForKey:@"modifier_cat"]valueForKey:@"cat_id"]objectAtIndex: section]];
NSArray *filteredArray = [[self.arrayDishDetails valueForKey: @"pre"] filteredArrayUsingPredicate: predicate];
return filteredArray;
希望这篇文章有所帮助。谢谢。
【讨论】:
谢谢!这对我有帮助。我得到了我的表视图的行数,如下所示: -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section NSPredicate *predicate = [NSPredicate predicateWithFormat:@"cat_id = %@", [[[self.arrayDishDetails valueForKey:@"modifier_cat"]valueForKey:@"cat_id"]objectAtIndex:section]]; NSArray *filteredArray = [[self.arrayDishDetails valueForKey:@"pre"] filteredArrayUsingPredicate:predicate];返回filteredArray.count; 但是我如何才能在我的 tableview 中为每个部分打印“prenm”? 您希望 prenm 作为部分标题还是行标题? 每个 prenm 作为每个部分的行。与第 0 节一样,所有 prenm 的 cat_id = 3,对于第 1 节,所有 prenm 的 cat_id = 6。 如果您觉得这个答案有帮助,请您接受并支持我的回答。这将有助于提高我的 SO 评级。@iHarshil【参考方案2】:所以你可以通过以下方式做到这一点:
for cat in [self.arrayDishDetails valueForKey:@"modifier_cat"]
NSString *cat_id = [cat valueForKey:@"cat_id"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"cat_id = %@", cat_id];
NSArray *filteredArray = [pre filteredArrayUsingPredicate:predicate];
//Here you will be getting your filtered array for cat_id 3 and 6 one after another, store it as per your requirement.
NSArray *filteredprenmArrayForCatId = [filteredArray valueForKey:@"prenm"];
希望这会有所帮助。
【讨论】:
以上是关于根据字典数组为 tableview 设置 numberOfRowsInSection的主要内容,如果未能解决你的问题,请参考以下文章
如何按月对 tableview 进行分类并将它们放在不同的部分?
在 Swift 中的数组中使用多个部分和多个字典填充 TableView