使用分段控件在 tableView 之间切换

Switch between tableViews using segmented Control

我有一个显示多个任务的 tableView,我想以编程方式在 2 个数据源之间切换。我创建了一个显示在顶部的分段控件,但是当我单击按钮时没有任何变化,我不知道如何 link 我的分段控件到我的数据源,这是我的代码:

class MyTasksCollectionCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
   
    var tasks = [Add]()
    var pastTasks = [Add]()
    
    static let identifier = "MyTasksCollectionCell"
    private let cellID = "CellID"
         
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tasks.count

        }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! MyTasksTableCell
        cell.accessoryType = .disclosureIndicator
             
        cell.categoryLabel.text =
            "\(tasks[indexPath.row].category)"
        cell.dateLabel.text =
            "\(tasks[indexPath.row].date)"
        cell.hourLabel.text =
            "\(tasks[indexPath.row].hour)"
        
        if cell.categoryLabel.text == "Urgent" {
            cell.categoryIcon.image = #imageLiteral(resourceName: "red.png")
        }
        if cell.categoryLabel.text == "Important" {
            cell.categoryIcon.image = #imageLiteral(resourceName: "orange.png")
        }
        if cell.categoryLabel.text == "Not Important" {
            cell.categoryIcon.image = #imageLiteral(resourceName: "green.png")
        }
        cell.dateIcon.image = UIImage(systemName: "calendar.badge.clock")

        return cell
          
    }
    
         func addControl() {
           let segmentItems = ["Present Tasks", "Past Tasks"]
           let control = UISegmentedControl(items: segmentItems)
           control.frame = CGRect(x: 10, y: 0, width: (self.tableView.frame.width - 20), height: 30)
           control.addTarget(self, action: #selector(segmentControl(_:)), for: .valueChanged)
           control.selectedSegmentIndex = 0
            tableView.addSubview(control)
        }

        @objc func segmentControl(_ segmentedControl: UISegmentedControl) {
           switch (segmentedControl.selectedSegmentIndex) {
              case 0:
                 // First segment tapped
                print("Present Tasks")
                self.tableView.reloadData()
              break
              case 1:
                 // Second segment tapped
                print("Past Tasks")
                 self.tableView.reloadData()
              break
              default:
              break
           }
        }
    }

使用枚举来了解根据段控制值在选项卡中显示的内容:

enum DispkayedTasks {
    case current
    case past
}
var displayedTask = DisplayedTasks.current

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int         {
    switch (displayedTask) {
          case .current:
             // First segment tapped
            return self.tasks.count
          case .past:
             // Second segment tapped
            return self.pastTasks.count
       }
    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! MyTasksTableCell
    cell.accessoryType = .disclosureIndicator
    let task = {() -> Add in
        switch (displayedTask) {
          case .current:
             // First segment tapped
            return self.tasks[indexPath.row]
          case past:
             // Second segment tapped
            return self.pastTasks[indexPath.row]
        }
    }()
         
    cell.categoryLabel.text =
        "\(task.category)"
    cell.dateLabel.text =
        "\(task.date)"
    cell.hourLabel.text =
        "\(task.hour)"
    
    if cell.categoryLabel.text == "Urgent" {
        cell.categoryIcon.image = #imageLiteral(resourceName: "red.png")
    }
    if cell.categoryLabel.text == "Important" {
        cell.categoryIcon.image = #imageLiteral(resourceName: "orange.png")
    }
    if cell.categoryLabel.text == "Not Important" {
        cell.categoryIcon.image = #imageLiteral(resourceName: "green.png")
    }
    cell.dateIcon.image = UIImage(systemName: "calendar.badge.clock")

    return cell
      
}

  @objc func segmentControl(_ segmentedControl: UISegmentedControl) {
       switch (segmentedControl.selectedSegmentIndex) {
          case 0:
             // First segment tapped
            print("Present Tasks")
            displayedTasks = .current
            self.tableView.reloadData()
       
          case 1:
             // Second segment tapped
            print("Past Tasks")
            displayedTasks = .past

             self.tableView.reloadData()
         
          default:
          break
       }
    }