Home > front end >  Switch between tableViews using segmented Control
Switch between tableViews using segmented Control

Time:11-25

I have a tableView showing multiple tasks and i would like to programmatically switch between 2 dataSources. I have created a segmented control that appear at the top but when i click on the buttons there is no change and i don't know how to link my segmented Control to my dataSources, here's my code:

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
           }
        }
    }

CodePudding user response:

Use an enum to know what to display in your tab depending on segment control value :

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
       }
    }
  • Related