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

问题描述 投票:1回答:1

我正在为两天的程序安排制作一个屏幕。我有一个ViewController,布局如下。

导航条--搜索条--分段控件--表视图。

在一个单独的文件中 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
    }   
}

我试了好几种方法,但还是不能让分段控制的方式发生变化,所以在 "全部 "中,它同时显示了两天的节头,第一天--只有第一天的节目和它的节头,第二天--只有第二天的节目和它的节头。有人能给我一个提示吗?也许我应该改变整个模型?

图片。

Image

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

ios swift uitableview uisegmentedcontrol
1个回答
1
投票

你需要更新你的 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()
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.