ios - How to navigate to viewcontrollers from expanded / nested UITableViewCells -
using alex di mango's popdownmenutable-master setup expanding nested uitableviewcell , functioning in project...but issue how navigate other view controllers menu , nested / expanding submenu uitableviewcells...using didselectrowatindexpth programatically created..please refer below code...
**popdcell.h** #import <uikit/uikit.h> @interface popdcell : uitableviewcell @property (strong, nonatomic) iboutlet uilabel *labeltext; @property (strong, nonatomic) iboutlet uilabel *separator; @property (strong, nonatomic) iboutlet uilabel *sepshadow; @property (strong, nonatomic) iboutlet uilabel *shadow; @end
popdcell.h
#import "popdcell.h" @implementation popdcell - (id)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier { self = [super initwithstyle:style reuseidentifier:reuseidentifier]; if (self) { // initialization code } return self; } -(void)prepareforreuse { self.backgroundcolor = [uicolor clearcolor]; } @end
popdsampleviewcontroller.m #import "popdsampleviewcontroller.h" #import "popdviewcontroller.h" static nsstring *kheader = @"menusectionheader"; static nsstring *ksubsection = @"menusubsection"; @interface popdsampleviewcontroller()<popddelegate> @end @implementation popdsampleviewcontroller - (void)viewdidload { [super viewdidload]; nsarray *sucsectionsa = [nsarray arraywithobjects:@"personal information",@"emergency contact", @"family physician", nil]; nsarray *sucsectionsb = [nsarray arraywithobjects:@"current medications",@"medical details", nil]; nsarray *sucsectionsc = [nsarray arraywithobjects:@"appointments",@"other reminders", nil]; nsarray *sucsectionsd = [nsarray arraywithobjects:@"health risk assessment",@"short hra", nil]; nsdictionary *section1 = [nsdictionary dictionarywithobjectsandkeys: @"dashboard", kheader, nil]; nsdictionary *sectiona = [nsdictionary dictionarywithobjectsandkeys: @"my profile", kheader, sucsectionsa, ksubsection, nil]; nsdictionary *section2 = [nsdictionary dictionarywithobjectsandkeys: @"my bmi", kheader, nil]; nsdictionary *sectionb = [nsdictionary dictionarywithobjectsandkeys: @"my medical status", kheader, sucsectionsb, ksubsection, nil]; nsdictionary *sectionc = [nsdictionary dictionarywithobjectsandkeys: @"my health planner", kheader, sucsectionsc, ksubsection, nil]; nsdictionary *sectiond = [nsdictionary dictionarywithobjectsandkeys: @"my health assessment", kheader, sucsectionsd, ksubsection, nil]; nsdictionary *section3 = [nsdictionary dictionarywithobjectsandkeys: @"my wellness tracker", kheader, nil]; nsarray *menu = [nsarray arraywithobjects: section1, sectiona, section2, sectionb, sectionc, sectiond, section3, nil]; popdviewcontroller *popmenu = [[popdviewcontroller alloc]initwithmenusections:menu]; popmenu.delegate = self; popmenu.view.frame = cgrectmake(0, 0, self.view.frame.size.width, self.view.frame.size.height); //ios7 status bar // popmenu.tableview.contentinset = uiedgeinsetsmake(20, 0, 0, 0); [self addchildviewcontroller:popmenu]; [self.view addsubview:popmenu.view]; } #pragma mark popdviewcontroller delegate -(void) didselectrowatindexpath:(nsindexpath *)indexpath{ nslog(@"didselectrowatindexpath: %ld,%ld",(long)indexpath.section,(long)indexpath.row); } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end
**popdviewcontroller.h** #import <uikit/uikit.h> #ifndef mb_strong #if __has_feature(objc_arc) #define mb_strong strong #else #define mb_strong retain #endif #endif #ifndef mb_weak #if __has_feature(objc_arc_weak) #define mb_weak weak #elif __has_feature(objc_arc) #define mb_weak unsafe_unretained #else #define mb_weak assign #endif #endif @protocol popddelegate <nsobject> -(void) didselectrowatindexpath:(nsindexpath *)indexpath; @end @interface popdviewcontroller : uitableviewcontroller - (id)initwithmenusections:(nsarray *) menusections; @property (mb_weak) id<popddelegate> delegate; @end
**popdviewcontroller.m** #import "popdviewcontroller.h" #import "popdcell.h" #import "tableviewcontroller.h" #import "popdsampleviewcontroller.h" #define tablecolor [uicolor colorwithred:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0] #define cellselected [uicolor colorwithred:4.0/255.0 green:198.0/255.0 blue:251.0/255.0 alpha:1.0] #define separator [uicolor colorwithred:124.0/255.0 green:130.0/255.0 blue:131.0/255.0 alpha:1.0] #define sepshadow [uicolor colorwithred:80.0/255.0 green:97.0/255.0 blue:110.0/255.0 alpha:1.0] #define shadow [uicolor colorwithred:69.0/255.0 green:84.0/255.0 blue:95.0/255.0 alpha:1.0] #define text [uicolor colorwithred:24.0/255.0 green:22.0/255.0 blue:121.0/255.0 alpha:1.0] static nsstring *kheader = @"menusectionheader"; static nsstring *ksubsection = @"menusubsection"; @interface popdviewcontroller () @property nsarray *sections; @property (strong, nonatomic) nsmutablearray *sectionsarray; @property (strong, nonatomic) nsmutablearray *showingarray; @end @implementation popdviewcontroller @synthesize delegate; - (id)initwithmenusections:(nsarray *) menusections { self = [super init]; if (self) { self.sections = menusections; } return self; } - (void)viewdidload { [super viewdidload]; uiedgeinsets inset = uiedgeinsetsmake(5, 0, 0, 0); self.tableview.contentinset = inset; // [[uitableview appearance] setseparatorcolor:[uicolor graycolor]]; self.tableview.tableheaderview = ({ uiview *view = [[uiview alloc] initwithframe:cgrectmake(0, 0, 0, 80.0f)]; uiimageview *imageview = [[uiimageview alloc] initwithframe:cgrectmake(-50, 0, 200, 80)]; imageview.autoresizingmask = uiviewautoresizingflexibleleftmargin | uiviewautoresizingflexiblerightmargin; imageview.image = [uiimage imagenamed:@"logo-hd.png"]; imageview.layer.maskstobounds = yes; // imageview.layer.cornerradius = 50.0; imageview.layer.bordercolor = [uicolor clearcolor].cgcolor; imageview.layer.borderwidth = 1.0f; imageview.layer.rasterizationscale = [uiscreen mainscreen].scale; imageview.layer.shouldrasterize = yes; imageview.clipstobounds = yes; [view addsubview:imageview]; view; }); self.tableview.backgroundcolor = tablecolor; [self.tableview setseparatorstyle:uitableviewcellseparatorstylesingleline]; self.tableview.frame = self.view.frame; self.sectionsarray = [nsmutablearray new]; self.showingarray = [nsmutablearray new]; [self setmenusections:self.sections]; } - (void)setmenusections:(nsarray *)menusections{ (nsdictionary *sec in menusections) { nsstring *header = [sec objectforkey:kheader]; nsarray *subsection = [sec objectforkey:ksubsection]; nsmutablearray *section = [nsmutablearray new]; [section addobject:header]; (nsstring *sub in subsection) { [section addobject:sub]; } [self.sectionsarray addobject:section]; [self.showingarray addobject:[nsnumber numberwithbool:no]]; } [self.tableview reloaddata]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return [self.sectionsarray count]; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if (![[self.showingarray objectatindex:section]boolvalue]) { return 1; } else{ return [[self.sectionsarray objectatindex:section]count];; } } - (void)tableview:(uitableview *)tableview willdisplaycell:(uitableviewcell *)cell forrowatindexpath:(nsindexpath *)indexpath { if(indexpath.row ==0){ if([[self.showingarray objectatindex:indexpath.section]boolvalue]){ [cell setbackgroundcolor:cellselected]; }else{ [cell setbackgroundcolor:[uicolor clearcolor]]; } } } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"menucell"; #warning : use here custom cell, instead of popdcell popdcell *cell = nil; cell = (popdcell *)[tableview dequeuereusablecellwithidentifier:cellidentifier]; nsarray *toplevelobjects = [[nsbundle mainbundle] loadnibnamed:@"popdcell" owner:self options:nil]; if (cell == nil) { cell = [toplevelobjects objectatindex:0]; } cell.labeltext.text = [[self.sectionsarray objectatindex:indexpath.section] objectatindex:indexpath.row]; cell.labeltext.textcolor = text; cell.separator.backgroundcolor = separator; cell.sepshadow.backgroundcolor = sepshadow; cell.shadow.backgroundcolor = shadow; // cell.layer.borderwidth = 0.5; [cell setselectionstyle:uitableviewcellselectionstylenone]; return cell; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath{ if([[self.showingarray objectatindex:indexpath.section]boolvalue]){ [self.showingarray setobject:[nsnumber numberwithbool:no] atindexedsubscript:indexpath.section]; }else{ [self.showingarray setobject:[nsnumber numberwithbool:yes] atindexedsubscript:indexpath.section]; } [tableview reloadsections:[nsindexset indexsetwithindex:indexpath.section] withrowanimation:uitableviewrowanimationfade]; [self.delegate didselectrowatindexpath:indexpath]; } @end
in didselectrowatindexpath
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { [self.tableview deselectrowatindexpath:indexpath animated:yes]; if (indexpath.row ==0) { [self performseguewithidentifier:@"segueidentifier0" sender:self]; }else if(indexpath.row==1{ [self performseguewithidentifier:@"segueidentifier1" sender:self]; }//and on }
depending on how many segues have continue. in storyboard need increase number of prototype cells going attributes inspector when selecting tableview. need control-click-drag viewcontrollers wish segue , select push. name identifiers "segueidentifer0"...
then need prepareforseque method
- (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([segue.identifier isequaltostring:@"segueidentifier0"]) { nslog(@"segue1"); }else if ([segue.identifier isequaltostring:@"segueidentifier1"]){ nslog(@"segue2"); } }
Comments
Post a Comment