ios - Tableview data won't load when unsegmented control is changed -


//the tableview not changing. have been @ days. seems simple. thank help

#import "stopstableviewcontroller.h"  static nsstring *myidentifier = @"routelisting";  @interface stopstableviewcontroller ()  @property (strong, nonatomic) nsarray *testarray;  @end   @implementation stopstableviewcontroller  - (instancetype)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil {     self = [super initwithnibname:nibnameornil bundle:nibbundleornil];      if (self) {         //set tab bar item's title         self.title = @"stops";      }              self.stoplist = [[api sharedapi] fetchstoplisting];      self.testarray = @[@"josh", @"kyle", @"nate"];      return self; }  - (void)viewdidload {     [super viewdidload];      [self.tableview registerclass:[uitableviewcell class] forcellreuseidentifier:myidentifier];      // adds segmented control     [self segmentedview];      }  - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }  - (void) segmentedview {     nsarray *segmentedmenu = [nsarray arraywithobjects:@"all", @"near me", nil];     uisegmentedcontrol *segmentedcontrol = [[uisegmentedcontrol alloc] initwithitems:segmentedmenu];     [segmentedcontrol addtarget:self                          action:@selector(valuechanged:)                forcontrolevents:uicontroleventvaluechanged];      segmentedcontrol.selectedsegmentindex = 0;     self.navigationitem.titleview = segmentedcontrol;  } 

pragma mark - table view data source

- (nsinteger)numberofsectionsintableview:(uitableview *)tableview {     // return number of sections.     return 1; }  - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {     if ([self.segmentedcontrol selectedsegmentindex] == 0) {         return  [self.stoplist count];      }     else {         return [self.testarray count];      } }   - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {      uitableviewcell *cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:myidentifier];     cell.accessorytype = uitableviewcellaccessorydisclosureindicator;      if (_segmentedcontrol.selectedsegmentindex == 0) {              cell.textlabel.text = [[self.stoplist objectatindex:indexpath.row] objectforkey:@"stoptitle"];             cell.detailtextlabel.text = [nsstring stringwithformat:@"stop %@", [[self.stoplist objectatindex:indexpath.row] objectforkey:@"stopnumber"]];     }      else {         cell.textlabel.text = self.testarray[indexpath.row];      }      return cell; } 

pragma mark - table view delegate

// in xib-based application, navigation table can handled in -tableview:didselectrowatindexpath: - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {     // navigation logic may go here, example:     // create next view controller.     stopinfoviewcontroller *detailviewcontroller = [[stopinfoviewcontroller alloc] initwithnibname:@"stopinfoviewcontroller" bundle:nil];      // pass selected object new view controller.      detailviewcontroller.stopinfo = [[self.stoplist objectatindex:indexpath.row] objectforkey:@"stopnumber"];      detailviewcontroller.stopname = [[self.stoplist objectatindex:indexpath.row] objectforkey:@"stoptitle"];       // push view controller.     [self.navigationcontroller pushviewcontroller:detailviewcontroller animated:yes]; }   //function segmentedview -(void) valuechanged:(uisegmentedcontrol *)sender {      [self.tableview reloaddata];     nslog(@"i'm getting called segment number is: %ld", (long)sender.selectedsegmentindex);  }  @end 

check datasource array before calling table reload method , make sure array contains new values corresponding segment have selected.


Comments