ios - How to utilize a UISegmentedControl With a UITableViewController? -
i have uitableviewcontroller
has 5 different sections, & each section contains individual cell. in section 1 have uisegmentedcontrol
that offers 2 options default segment 1 picked rest of sections (the other 4) correspond segment 1. however, problem having is: having ability switch uitableview
has other sections & cells correspond segment 2, when of course segment 2 selected in uisegmentedcontrol
i using storyboards project.
i know question of how achieve has been asked thousand times, , know how if working regular view controllers, new working uitableviewcontroller
, giving me hard time.
things have tried:
no.1: tried deleting uitableviewcontroller
, replacing normal viewcontroller
, sub uitableview
main view. problem given error xcode because static cells require uitableviewcontroller
work, (unfortunately working static cells).
no.2: have tried hide cells conditional uisegmentedcontrol
display appropriate cells appropriate selection, did not work because not able add other sections & cells on top (as have done regular uiviewcontroller
.
this code used try method # 2 :
- (ibaction)segmentedcontrol:(id)sender { uisegmentedcontrol *segmentedcontrol = (uisegmentedcontrol *) sender; nsinteger selectedsegment = segmentedcontrol.selectedsegmentindex; if (selectedsegment == 0) { upick= 0; self.lengthcell.hidden=no; self.widthcell.hidden=no; self.depthcell.hidden=no; self.flowcell.hidden=yes; } else if (selectedsegment == 1) { upick =1; self.lengthcell.hidden=yes; self.widthcell.hidden=yes; self.depthcell.hidden=yes; self.flowcell.hidden=no; } }
no.3: countless hours of research on net can't seem find exact results looking for, show how uiviewcontroller
, steps different particular case.
i appreciate advice & overall direction take here because stuck. guys!
have cells need available in table view controller.
in table view's datasource methods (i.e.
numberofsections
,numberofrowsinsection
,cellforrowatindexpath
) can set correct numbers , data 2 states.when segmented control changes, can 2 things. either
reloaddata
, suggested before, not animations. or useinsertrowsatindexpaths:withrowanimation:
,deleterowsatindexpaths:withrowanimation:
animation effect.
e.g. showing sections 1-2 in case 1 , sections 3-6 in case 2 (section 0 being top section); each section has 2 rows
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return _control.selectedsegmentindex == 0 ? 3 : 4; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return section == 0 ? 1 : 2; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { // .. usual boilerplate if (_control.selectedsegmentindex == 0) { /* configure cell */ } else { /*do same other case */ } return cell; } -(void)segmentedcontroldidchange:(uisegmentedcontrol*)sender { nsindexset *indexes; [_tableview beginupdates]; if (sender.selectedsegmentindex == 0) { indexes = [nsindexset indexsetwithindexesinrange:nsmakerange(1,4)]; [_tableview deletesections:indexes withrowanimation: uitableviewrowanimationfade]; indexes = [nsindexset indexsetwithindexesinrange:nsmakerange(1,2)]; [_tableview insertsections:indexes withrowanimation: uitableviewrowanimationfade]; [_tableview reloadsections:indexes withrowanimation: uitableviewrowanimationnone]; } else { // reverse } [_tableview endupdates]; }
Comments
Post a Comment