我有一个 UITableViewController
它有 5 个不同的部分,每个部分包含一个单独的单元格。在第 1 节中,我有一个 UISegmentedControl
默认情况下提供两个选项段 1 被选中,因此其余部分(其他 4 个)对应于段 1。但是,我遇到的问题是:能够切换到另一个 UITableView
具有与段 2 相对应的其他部分和单元格,当然,在 UISegmentedControl
中选择了段 2 时。
我正在为这个项目使用 Storyboard。
我知道如何实现这一点的问题已经被问了一千次,如果我使用常规 View Controller ,我知道如何做到这一点,我是使用 UITableViewController
的新手这真的让我很难过。
我尝试过的事情:
1号:我尝试删除 UITableViewController
全部放在一起并替换为正常的ViewController
和子 UITableView
到主视图。问题是 xcode 给了我一个错误,因为静态单元格需要 UITableViewController
工作,(不幸的是,我正在使用静态单元格)。
2号:我试图用 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
.
这是我用来尝试方法#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;
}
}
3号:在网上进行了无数小时的研究,但似乎找不到我正在寻找的确切结果,大多数都显示了如何使用
UIViewController
进行此操作对于这种特殊情况,步骤是不同的。我真的很感激从这里得到的一些建议和总体方向,因为我被困住了。多谢你们!
最佳答案
numberOfSections
、 numberOfRowsInSection
、 cellForRowAtIndexPath
)中,您可以为两种状态设置所有正确的数字和数据。 reloadData
,正如之前建议的那样,但是你没有得到任何动画。或者您使用 insertRowsAtIndexPaths:withRowAnimation:
和 deleteRowsAtIndexPaths:withRowAnimation:
获得动画效果。 例如。显示案例 1 中的第 1-2 部分和案例 2 中的第 3-6 部分(第 0 部分是顶部);每个部分有 2 行
- (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];
}
关于ios - 如何将 UISegmentedControl 与 UITableViewController 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17715770/