ios - 如何将 UISegmentedControl 与 UITableViewController 结合使用?

标签 ios objective-c uitableview uisegmentedcontrol

我有一个 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 进行此操作对于这种特殊情况,步骤是不同的。

我真的很感激从这里得到的一些建议和总体方向,因为我被困住了。多谢你们!

最佳答案

  • 在表格 View Controller 中提供您需要的所有单元格。
  • 在表格 View 的数据源方法(即 numberOfSectionsnumberOfRowsInSectioncellForRowAtIndexPath )中,您可以为两种状态设置所有正确的数字和数据。
  • 当分段控件发生变化时,您可以做两件事。要么你只是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/

    相关文章:

    ios - 以编程方式添加 subview 后自动调整单元格大小

    ios - 将 cocoapods 添加到使用 cordova 6.5 创建的 iOS 工作区

    ios - Xcode 9 上的 XS Max 模拟器?

    ios - 在 binutils 中运行 objdump 时无法识别文件格式

    iPhone SDK 屏幕尺寸使用相似的语法返回不同的结果

    ios - 在 UITableViewCell 中使用 Timer 在 UILabel 中设置时间

    ios - 类扩展中声明的变量和实现类中的变量有什么区别

    ios - 我想从解析关系中查询对象

    objective-c - Obj-C __block 变量保留行为

    ios - UITableView:在构建表格后更改一个单元格的高度约束?