【问题标题】:How to toggle data showed in a table view如何切换表格视图中显示的数据
【发布时间】:2016-06-28 11:27:14
【问题描述】:

在我的应用程序中,我的 ParentViewController 中有一个分段控件和一个容器。容器中有一个名为 ChildTableViewController 的 TableViewController。我需要根据分段控件更改表中的数据。

问题:我有一个段控制操作设置,但我不确定如何引用子项来运行 reloadData() 或在更改段控制时以某种方式在 TableViewController 中运行触发器。

//MARK: IBActions
    @IBAction func segmentedControlAction(sender: AnyObject) {
        print(segmentedControl.selectedSegmentIndex)

    }

此代码根据我单击的部分打印 0、1 或 2

【问题讨论】:

    标签: xcode swift uitableview uisegmentedcontrol


    【解决方案1】:

    在您的segmentControlTapped 操作方法中,您将获得UISegmentedControlselectedSegmentIndex 属性。使用此属性值来切换tableViewdataSource,如下所示:

    switch selectedSegmentIndex {
        case 0: //Segment 1
              self.dataSource = /*update your dataSource here for Segment 1*/
    
        case 1: //Segment 2
              self.dataSource = /*update your dataSource here for Segment 2*/
    
        case 2: //Segment 3
              self.dataSource = /*update your dataSource here for Segment 3*/
    
        default: break
    
       /*IMPORTANT*/
       self.tableView.reloadData()
    }
    

    【讨论】:

    • 那么这个去哪儿了? ChildTableViewController 还是 ParentViewController?自动完成在 ChildViewController 中找不到任何名为 selectedSegmentIndex 的内容。
    • 为您的ChildTableViewControllers tableView
    • 告诉我你的action 方法segmentControl 和受人尊敬的tableView
    • 将您的 sender: AnyObject 更改为 sender: UISegmentedControl
    • 这是做什么的?该操作以正确的数字触发。我只需要将这个数字传递给 ChildTableViewController。
    猜你喜欢
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多