Reputation: 2450
I have a UITableViewController
that has 5 different sections, & each section contains an individual cell. In section 1 I have a UISegmentedControl
that offers two options by default segment 1 is picked so the rest of the sections (the other 4) correspond with segment 1. However, the problem that I am having is: Having the ability to switch to another UITableView
that has other sections & cells that would correspond to segment 2, when of course segment 2 is selected in the UISegmentedControl
I am using storyboards for this project.
I know that the question of how to achieve this has been asked a thousand times, and I know how to do this if I was working with regular view controllers, I am new to working with UITableViewController
and this is really giving me a hard time.
Things that I have tried:
No.1: I tried deleting the UITableViewController
all together and replacing with a normal ViewController
and sub a UITableView
to the main view. The problem with that is that I was given an error by xcode because static cells require a UITableViewController
to work, (unfortunately I am working with static cells).
No.2: I have tried to hide the cells with a conditional from the UISegmentedControl
to only display the appropriate cells with the appropriate selection, but this did not work because I was not able to add my other sections & cells on top (as I would have normally done with regular UIViewController
.
This is the code that I used to 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 the net but can't seem to find the exact results I am looking for, most show how to do this with a UIViewController
and the steps are different for this particular case.
I would really appreciate some advice & overall direction to take from here because I am stuck. Thanks guys!
Upvotes: 1
Views: 3093
Reputation: 80265
Have all the cells you need available in your table view controller.
In your table view's datasource methods (i.e. numberOfSections
, numberOfRowsInSection
, cellForRowAtIndexPath
) you can set all the correct numbers and data for the two states.
When the segmented control changes, you can do two things. Either you simply reloadData
, as was suggested before, but then you do not get any animations. Or you use insertRowsAtIndexPaths:withRowAnimation:
and deleteRowsAtIndexPaths:withRowAnimation:
to get the animation effect.
E.g. showing sections 1-2 in case 1 and sections 3-6 in case 2 (section 0 being the 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 {
// .. the usual boilerplate
if (_control.selectedSegmentIndex == 0) { /* configure cell */ }
else { /*do the same for the 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 {
// do the reverse
}
[_tableView endUpdates];
}
Upvotes: 3
Reputation: 650
If I am correct, you need to switch between 2 table views using UISegmentedControl. You can use UIViewController and add 2 UITableViews to it and keep your UISegmentedControl outside the table views. Then may be you can manage it in the following method:
- (IBAction)segmentedControl:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
if (selectedSegment == 0)
{
//show Table1 and hide Table2
// Reload Table1
}
else if (selectedSegment == 1)
{
//show Table2 and hide Table1
// Reload Table2
}
}
Hope this helps.
Upvotes: 0