gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:49:07 27 4
gpt4 key购买 nike

我正在为两天的节目时间表制作一个屏幕。我有一个具有以下布局的ViewController:

NavigationBar-SearchBar-分段控件-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
}
}

我尝试了几种方法,但是仍然无法使分段控制切换,因此在“全部”中,两天都显示了它们的节头,第一天-只有第一天程序带有节头,第二天-只有第二天程序及其节标题。有人可以给我提示该怎么做吗?也许我应该改变整个模型?

图片:

当我在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()
}

关于ios - 如何使分段控件显示不同数量的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61822473/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com