简体   繁体   English

根据字典数组为tableview设置numberOfRowsInSection

[英]Set numberOfRowsInSection for tableview as per the Array of Dictionaries

I have a quick question here... 我在这里有一个简单的问题......

Here I have an Array named arrayDishDetails 这里我有一个名为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"
        }
    ]
}

Now in my tableview, I set titleForHeaderInSection like this. 现在在我的tableview中,我像这样设置了titleForHeaderInSection

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[[self.arrayDishDetails valueForKey:@"modifier_cat"] valueForKey:@"cat_name"] objectAtIndex:section];
}

I set numberOfSectionsInTableView like this.. 我像这样设置numberOfSectionsInTableView ..

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.arrayDishDetails valueForKey:@"modifier_cat"] count];
}

Now, My question is, How can I filter the "pre" Array to get "prenm" by "cat_id" from "modifier_cat" Array so that I can get the 2 different Categories? 现在,我的问题是,如何过滤“pre”数组,从“modifier_cat”数组中获取“cat_id”的 “prenm” ,这样我就可以得到2个不同的类别?

For Example :- 例如 :-

for "cat_id" = "3" I want all the "prenm" like "prenm": "BATTER FRIED FISH","JALAPENO POPPERS", "CHICKEN SAUSAGES", etc from "pre" Array. 对于“cat_id”=“3”我希望所有的“prenm”像“prenm”:“BATTER FRIED FISH”,“JALAPENO POPPERS”,“CHICKEN SAUSAGES”等来自“pre” Array。

And same for "cat_id" = "6" 同样的“cat_id”=“6”

I have looked for many solutions to the filter Array but can't get help for mine. 我已经寻找过滤器阵列的许多解决方案,但无法获得我的帮助。 Any help would be much appreciated. 任何帮助将非常感激。 Thanks! 谢谢!

Edit 1: 编辑1:

I got some hint from the answer of @Karthik and I set my numberofrows for my tableview like this: 我从@Karthik的答案得到了一些提示,我为我的tableview设置了我的numberofrows,如下所示:

-(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; 
}

Now, how can I set the "prenm" for each section (here 2 sections 0 and 1) as per "cat_id" of "modifier_cat" Array? 现在,我如何根据“modifier_cat”数组的“cat_id”为每个部分(这里是2个0和1部分)设置“prenm”

I think you are looking on to filtering array with custom objects. 我认为您正在寻找使用自定义对象过滤数组。 I am assuming the JSON you have put are mapped to custom objects. 我假设您放置的JSON映射到自定义对象。 Add the below code to filter pre array with cat_id from modifier_cat array. 添加以下代码以使用modifier_cat数组中的cat_id过滤预数组。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self getFilteredArray: section].count;
}

And to display the cell with items please use the below method 要显示带有项目的单元格,请使用以下方法

- (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;
}

// Method to reuse for filtering array //重用过滤数组的方法

- (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;
}

Hope this one helps. 希望这个有所帮助。 Thanks. 谢谢。

So you can do that the below way: 所以你可以通过以下方式做到这一点:

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM