根据字典数组为表视图设置 numberOfRowsInSection
Set numberOfRowsInSection for tableview as per the Array of Dictionaries
我有一个简短的问题...
这里我有一个名为 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" 数组以通过 [=38= 得到 "prenm" ]"cat_id" 来自 "modifier_cat" 数组以便我可以获得 2 个不同的类别?
例如:-
for "cat_id" = "3" 我想要所有 "prenm" 像 "prenm": " BATTER FRIED FISH","JALAPENO POPPERS", "CHICKEN SAUSAGES", etc from "pre" Array.
cat_id" = "6"
也一样
我已经为过滤器数组寻找了很多解决方案,但无法为我找到帮助。
任何帮助将非常感激。谢谢!
编辑 1:
我从@Karthik 的回答中得到了一些提示,我为我的 tableview 设置了我的行数,如下所示:
-(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;
}
现在,我如何根据 "cat_id" 为每个部分(这里有 2 个部分 0 和 1)设置 "prenm" modifier_cat 数组?
我认为您正在寻找带有自定义对象的过滤数组。我假设您放置的 JSON 已映射到自定义对象。添加以下代码以使用 modifier_cat 数组中的 cat_id 过滤预数组。
- (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;
}
希望这对您有所帮助。谢谢
所以你可以通过以下方式做到这一点:
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"];
}
希望对您有所帮助。
我有一个简短的问题...
这里我有一个名为 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" 数组以通过 [=38= 得到 "prenm" ]"cat_id" 来自 "modifier_cat" 数组以便我可以获得 2 个不同的类别?
例如:-
for "cat_id" = "3" 我想要所有 "prenm" 像 "prenm": " BATTER FRIED FISH","JALAPENO POPPERS", "CHICKEN SAUSAGES", etc from "pre" Array.
cat_id" = "6"
也一样我已经为过滤器数组寻找了很多解决方案,但无法为我找到帮助。 任何帮助将非常感激。谢谢!
编辑 1:
我从@Karthik 的回答中得到了一些提示,我为我的 tableview 设置了我的行数,如下所示:
-(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;
}
现在,我如何根据 "cat_id" 为每个部分(这里有 2 个部分 0 和 1)设置 "prenm" modifier_cat 数组?
我认为您正在寻找带有自定义对象的过滤数组。我假设您放置的 JSON 已映射到自定义对象。添加以下代码以使用 modifier_cat 数组中的 cat_id 过滤预数组。
- (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;
}
希望这对您有所帮助。谢谢
所以你可以通过以下方式做到这一点:
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"];
}
希望对您有所帮助。