ios - UITableView - switch data from two arrays -
i'm working on application in have uitableview, 2 arrays , 1 uisegmentedcontrol. need when uisegmentedcontrol value 0 loaded in uitableview data array one, , when uisegmentedcontrol has value 1 loaded data array two. simply, need switch data loaded uitableview arrays. tried use bool, not work, think it's not ideal.
here code:
- (void)viewdidload { [super viewdidload]; alltabledata = [[nsmutablearray alloc] initwithobjects: [[food alloc] initwithname:@"bwm" anddescription:@"auto" anddefinice:@"osobni"],nil ]; alltabledata2 = [[nsmutablearray alloc] initwithobjects: [[food alloc] initwithname:@"renault" anddescription:@"dodavka" anddefinice:@"velka"],nil ]; } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { long rowcount; if(self.isswich == false) rowcount = alltabledata.count; else rowcount = alltabledata2.count; return rowcount; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier]; food* food; if(self.isswitch == false) { food = [alltabledata objectatindex:indexpath.row]; } else { food = [alltabledata2 objectatindex:indexpath.row]; } cell.textlabel.text = food.name; cell.detailtextlabel.text = food.description; return cell; } -(ibaction)switchdata:(id)sender { if(self.myswitcher.selectedsegmentindex == 0) { isswitch = false; } else { isswitch = true; } }
you missing call reloaddata
in witchdata:
implementation. adding call fix problem.
i think it's not ideal.
that's right. rather storing flag says array use data source, store array itself. eliminate if
s on isswitch
property:
// declare instance variable, , use // in data source methods. nsarray *thesource; - (void)viewdidload { [super viewdidload]; alltabledata = [[nsmutablearray alloc] initwithobjects: [[food alloc] initwithname:@"bwm" anddescription:@"auto" anddefinice:@"osobni"],nil ]; alltabledata2 = [[nsmutablearray alloc] initwithobjects: [[food alloc] initwithname:@"renault" anddescription:@"dodavka" anddefinice:@"velka"], nil]; thesource = alltabledata; } -(ibaction)switchdata:(id)sender { if(self.myswitcher.selectedsegmentindex == 0) { thesource = alltabledata; } else { thesource = alltabledata2; } [mytable reloaddata]; }
alternatively make isswitch
integer, , use index array has alltabledata
@ index 0 , alltabledata2
@ index one:
nsarray *sources; int sourceindex; // use sources[sourceindex] current source - (void)viewdidload { [super viewdidload]; alltabledata = [[nsmutablearray alloc] initwithobjects: [[food alloc] initwithname:@"bwm" anddescription:@"auto" anddefinice:@"osobni"], nil]; alltabledata2 = [[nsmutablearray alloc] initwithobjects: [[food alloc] initwithname:@"renault" anddescription:@"dodavka" anddefinice:@"velka"],nil ]; sources = @[alltabledata, alltabledata2]; sourceindex = 0; } -(ibaction)switchdata:(id)sender { sourceindex = self.myswitcher.selectedsegmentindex; [mytable reloaddata]; }
Comments
Post a Comment