ios - how to load different data into the table view from different arrays depending on what row is clicked using storyboards on iOS7? -
ios - how to load different data into the table view from different arrays depending on what row is clicked using storyboards on iOS7? -
i learning ios development , working on sample ios application. got doubts in uitableview, wish load different info table view different arrays depending on row clicked. done using xcode 4 nib files, have written didselectrowatindexpath method in implementation file
-(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { petstableviewcontroller *pets = [[petstableviewcontroller alloc]initwithnibname:@"petstableviewcontroller" bundle:nil]; if([[petsarray objectatindex:indexpath.row ]isequal:@"dog"]) { pets.petsint = 0; [pets settitle:[petsarray objectatindex:indexpath.row]]; } if([[petsarray objectatindex:indexpath.row ]isequal:@"cat"]) { pets.petsint = 1; [pets settitle:[petsarray objectatindex:indexpath.row]]; } if([[petsarray objectatindex:indexpath.row ]isequal:@"snake"]) { pets.petsint = 2; [pets settitle:[petsarray objectatindex:indexpath.row]]; } [self.navigationcontroller pushviewcontroller:pets animated:yes]; // [pets release]; } this sample code, here declared dog, cat, snake arrays in table 1. when clicked of row should display
dog -> dog1, dog2, dog3 arrays each row in different table (table 2).
how using storyboard xcode 5 ? (using segues )
please share thoughts
first, allow me simplify code
-(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { petstableviewcontroller *pets = [[petstableviewcontroller alloc]initwithnibname:@"petstableviewcontroller" bundle:nil]; if([petsarray[indexpath.row] isequaltostring:@"dog"]) { pets.petsint = 0; [pets settitle:petsarray[indexpath.row]]; } if([petsarray[indexpath.row] isequaltostring:@"cat"]) { pets.petsint = 1; [pets settitle:petsarray[indexpath.row]]; } if([petsarray[indexpath.row] isequaltostring:@"snake"]) { pets.petsint = 2; [pets settitle:petsarray[indexpath.row]]; } [self.navigationcontroller pushviewcontroller:pets animated:yes]; } second, if want init controller you've created on storyboard, should utilize line of code:
petstableviewcontroller *pets = [[uistoryboard storyboardwithname:@"main" bundle:[nsbundle mainbundle]] instantiateviewcontrollerwithidentifier:@"petstableviewcontroller"];
remember set storyboard id
third, if want pushed view controller display dog1, dog2, dog3 on table, create nsarray property in pettableviewcontroller.h, utilize normal.
fourth, code should works fine, don't need utilize segue in case. however, if want utilize segue, open storyboard , click on prototype uitableviewcell , ctrl-drag pettableviewcontroller, set segue push.
in controller, implement prepareforsegue method, set petarray destinationviewcontroller , that's it.
//-------------------------------------- update:
you setup petviewcontroller in storyboard wrong, delete , add together uitableviewcontroller
you forget set storyboard id in mainstoryboard, line not work petstabletableviewcontroller *pets = [[uistoryboard storyboardwithname:@"main" bundle:[nsbundle mainbundle]] instantiateviewcontrollerwithidentifier:@"petstabletableviewcontroller"];
try find in storyboard.
don't drag uitableviewcell petviewcontroller (i wrong, because used once). instead, drag viewcontroller petviewcontroller. if properly, these lines of code should work fine:
petstabletableviewcontroller *pets = [[uistoryboard storyboardwithname:@"main" bundle:[nsbundle mainbundle]] instantiateviewcontrollerwithidentifier:@"petstabletableviewcontroller"]; [self.navigationcontroller pushviewcontroller:pets animated:yes]; or using segue
-(void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender{ if([segue.identifier isequaltostring:@"showlistdetails"]) { petstabletableviewcontroller *destviewcontroller = segue.destinationviewcontroller; [destviewcontroller setpetsint:selectedindex.row]; //selectedindex class variable of nsindexpath } } -(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { selectedindex = indexpath; [self performseguewithidentifier:@"showlistdetails" sender:self]; } ios iphone uitableview ios7 uistoryboardsegue
Comments
Post a Comment