ios - Show Category and subcategory in tableview -
i have 2 arrays: 1 categories , second subcategory. when parse category , save in first array , subcategory array, want access specific subcategories category. e.g 1 index of category array want save subcategory array that.
nsdictionary * datas = [dic valueforkey:@"data"]; for(nsarray *data in datas ){ parsesubcategorymodel * pscm = [[parsesubcategorymodel alloc]init]; pscm.zeroindex = [data objectatindex:0]; pscm.oneindex = [data objectatindex:1]; pscm.twoindex = [data objectatindex:2]; pscm.threeindex = [data objectatindex:3]; nsarray *sub1 = [ pscm.oneindex valueforkey:@"sub_categories"];// parse sub_categories here ok yaha tak self.fetchdata = [[nsmutablearray alloc]init]; self.subsubdata = [[nsmutablearray alloc]init]; for(nsdictionary *subdic in sub1 ){ parsesubcategorymodel * pscm1 = [[parsesubcategorymodel alloc]init]; pscm1.id_no = [subdic valueforkey:@"_id"]; pscm1.name = [subdic valueforkey:@"name"]; pscm1.is_active = [subdic valueforkey:@"is_active"]; pscm1.url = [subdic valueforkey:@"url"]; pscm1.category_order = [subdic valueforkey:@"sub_category_order"]; pscm1.updated_at = [subdic valueforkey:@"updated_at"]; pscm1.created_at = [subdic valueforkey:@"created_at"]; [self.fetchdata addobject:pscm1]; nsarray *subsub1 = [subdic valueforkey:@"sub_subcategories"];//parse sub_subcategories here for(nsdictionary *subsubdic in subsub1 ){ parsesubcategorymodel * pscm2 = [[parsesubcategorymodel alloc]init]; pscm2.id_no = [subsubdic valueforkey:@"_id"]; pscm2.name = [subsubdic valueforkey:@"name"]; pscm2.is_active = [subsubdic valueforkey:@"is_active"]; pscm2.url = [subsubdic valueforkey:@"url"]; pscm2.category_order = [subsubdic valueforkey:@"sub_subcategory_order"]; pscm2.updated_at = [subsubdic valueforkey:@"updated_at"]; pscm2.created_at = [subsubdic valueforkey:@"created_at"]; [self.subsubdata insertobject:pscm2 atindex:0]; nslog(@"%@",self.subsubdata); } } }
it bit easy approach solve requirement using uitableview section header , cells:
i have tried method expand , collapse of uitableview well:
@interface viewcontroller ()<uitableviewdelegate ,uitableviewdatasource> { nsarray *categoryarray; nsarray *subcategoryarray; //this dictionary used add subcategory array each category using category id. nsmutabledictionary *subcategorydict; } @end - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { //returns number of category array section return categoryarray.count; }
//take nsdictionary or custom object category id pass inside our dictionary subcategory array
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { nsdictionary *data = [categoryarray objectatindex:section]; nsarray *subcategory = [subcategorydict valueforkey:[data valueforkeypath:@"_categoryid"]]; return subcategory.count; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"identifier" forindexpath:indexpath]; nsdictionary *data = [categoryarray objectatindex:indexpath.section]; nsarray *subcategory = [subcategorydict valueforkey:[data valueforkeypath:@"_categoryid"]]; nsdictionary *subcatdata = [subcategory objectatindex:indexpath.row]; cell.textlabel.text = [subcatdata valueforkey:@"values"]; return cell; }
you can use uitableview cell customisation of header section. need create uibutton , add subview section view selection make sure have added tag value section.
- (uiview *)tableview:(uitableview *)tableview viewforheaderinsection:(nsinteger)section { uiview *view = [[uiview alloc]initwithframe:cgrectmake(0, 0, self.view.frame.size.width, 40)]; nsdictionary *data = [categoryarray objectatindex:section]; return view; }
hope work per requirements.
if want use custom uitableview can below code: https://github.com/genkernel/treeview
Comments
Post a Comment