Ios 如何使分段控件显示不同数量的单元格?
我正在为两天的节目安排做一个屏幕。我有一个具有以下布局的ViewController: 导航栏-搜索栏-分段控件-表格视图 在一个单独的文件Ios 如何使分段控件显示不同数量的单元格?,ios,swift,uitableview,uisegmentedcontrol,Ios,Swift,Uitableview,Uisegmentedcontrol,我正在为两天的节目安排做一个屏幕。我有一个具有以下布局的ViewController: 导航栏-搜索栏-分段控件-表格视图 在一个单独的文件UITableViewCell中,我绘制了一个自定义单元格。我的VC中的主要逻辑: struct Schedule { var time: String var title: String } struct SectionForDay { let sectionTitle: String var dayProgram: [S
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
}
}
我尝试了几种方法,但仍然不能使分段控制切换的方式,因此,在所有它显示两天与他们的节标题,第1天-只有第一天的节目与它的节标题,第2天-只有第二天的节目与它的节标题。有谁能给我一个提示怎么办?也许我应该改变整个模型
图片:
当我在3个项目之间切换分段控件时,它总是显示两天。当分段控件值更改时,您需要更新
节
@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()
}
你能把你的问题重新表述一下吗?我不确定您试图用分段控件实现什么,并且您没有在代码中的任何地方使用它。@clawesome添加了代码和链接您想根据分段控件选择隐藏节吗?然后您需要更改部分
属性中的数据。当seg控件的值更改时,用这两天填充部分
,如果选择了特定的一天,则删除不是该天的部分。理想情况下,当您更改部分
中的数据时,您希望执行正确的表格更新,但要使表格反映更改,最简单的方法是调用它的重载数据
函数。