如何使分段控件显示不同数量的单元格?

How can I make a segmented control show different numbers of cells?

我正在制作两天节目表的屏幕。我有一个 ViewController 布局如下:

NavigationBar - SearchBar - Segmented control - TableView.

在一个单独的文件中 UITableViewCell 我绘制了一个自定义单元格。我的主要逻辑VC:

struct Schedule {
    var time: String
    var title: String
}

struct SectionForDay {
    let sectionTitle: String
    var dayProgram: [Schedule]
}

class ProgramViewController: UIViewController {

    var tableView = UITableView()
    let identifier = "Cell"

    var dayOne = [
      Schedule(time: "10:00 - 11:00", title: "DayOne SessionOne"), 
      Schedule(time: "11:00 - 12:00", title: "DayOne SessionTwo")
    ]

    var dayTwo = [
      Schedule(time: "22:00 - 23:00", title: "DayTwo SessionThree"), 
      Schedule(time: "24:00 - 01:00", title: "DayTwo SessionFour")
    ]

    var sections = [SectionForDay]()

    let segmentedControl: UISegmentedControl = {
        let sc = UISegmentedControl(items: ["All", "Day 1", "Day 2"])
        sc.selectedSegmentIndex = 0
        sc.addTarget(self, action: #selector(handleSegmentedChange), for: .valueChanged)
        return sc
    }()

   @objc func handleSegmentedChange() {
        switch segmentedControl.selectedSegmentIndex {
        case 0:
            dayToDisplay = dayOne + dayTwo
        case 1:
            dayToDisplay = dayOne
        default:
            dayToDisplay = dayTwo
        }
        tableView.reloadData()
    }

    lazy var dayToDisplay = dayOne + dayTwo

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(ProgramCell.self, forCellReuseIdentifier: identifier)
        sections = [
        SectionForDay(sectionTitle: "Day 1", dayProgram: dayOne),
        SectionForDay(sectionTitle: "Day 2", dayProgram: dayTwo)
        ]
    }

extension ProgramViewController: UITableViewDelegate, UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return self.sections.count
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return self.sections[section].sectionTitle
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                let items = self.sections[section].dayProgram
                return items.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! ProgramCell
        let items = self.sections[indexPath.section].dayProgram
        let currentDay = items[indexPath.row]
        cell.dateLabel.text = currentDay.time
        cell.titleLabel.text = currentDay.title
        return cell
    }   
}

我尝试了几种方法,但仍然无法使分段控制切换方式,因此在 All 中它显示了两天的部分 headers,第 1 天 - 只有第一天的节目它的部分 header,第 2 天 - 只有第二天的节目及其部分 header。谁能给我提示该怎么做?也许我应该改变整个模型?

图片:

当我在 3 个项目之间切换分段控件时,它总是显示两天。

您需要在分段控件值更改时更新您的 sections 数组。

@objc func handleSegmentedChange() {
    switch segmentedControl.selectedSegmentIndex {
    case 0:
        sections = [
            SectionForDay(sectionTitle: "Day 1", dayProgram: dayOne),
            SectionForDay(sectionTitle: "Day 2", dayProgram: dayTwo),
        ]
    case 1:
        sections = [
            SectionForDay(sectionTitle: "Day 1", dayProgram: dayOne),
        ]
    default:
        sections = [
            SectionForDay(sectionTitle: "Day 2", dayProgram: dayTwo),
        ]
    }
    tableView.reloadData()
}