gpt4 book ai didi

ios - 如何在 TableView 部分使用一个带有可选用例的枚举

转载 作者:行者123 更新时间:2023-12-01 16:13:13 25 4
gpt4 key购买 nike

我有3个部分显示可选变量是否不是nil,如果只有2个。
我想要的是将所有这些都放在一个枚举(或struct中,如果不可能的话)。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let currentTraining = self.currentTraining {
switch indexPath.section {
case 0:
return currentTrainingCell(tableView, indexPath, currentTraining: currentTraining)
case 1:
return qrCodeCell(tableView, indexPath)
case 2:
return trainingMethodCell(tableView, indexPath)
default:
fatalError("No more sections allowed")
}
} else {
switch indexPath.section {
case 0:
return qrCodeCell(tableView, indexPath)
case 1:
return trainingMethodCell(tableView, indexPath)
default:
fatalError("No more sections allowed")
}
}
}

我想到了将所有这些都包装在枚举(或结构,如果更有意义的话)中,然后切换大小写并在 cellForRow中缩短我的代码
enum TrainingSection {
case qrCode
case trainingMethod
// if its nil to make nothing if yes do call the method
case currentTraining(FTCurrentTraining)
}

最佳答案

在某些情况下,您似乎打算隐藏一个部分。典型的方法是将行高设置为零。您是正确的,通常应为部分ID使用枚举。例如,您可以执行以下操作:

enum TrainingSection: Int {
case currentTraining
case qrCode
case trainingMethod
}

var currentTraining: FTCurrentTraining?

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch TrainingSection(rawValue: indexPath.section)! {
case .currentTraining:
if let currentTraining = currentTraining {
return currentTrainingCell(tableView, indexPath, currentTraining: currentTraining)
} else {
return UITableViewCell() // Just return an empty cell
}
case .qrCode:
return qrCodeCell(tableView, indexPath)
case .trainingMethod:
return trainingMethodCell(tableView, indexPath)
}
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch TrainingSection(rawValue: indexPath.section)! {
case .currentTraining where currentTraining == nil:
return 0
default:
return tableView.rowHeight
}
}

具有关联数据的枚举在这里是错误的工具。从概念上讲,存在节标识符,并且完全分开存在一个状态( currentTraining)。在这种情况下,合并这些没有意义。也就是说,您可以采用完全不同的方式并根据您的状态来重新构建部分。这在某些情况下非常有用,但在这里我认为它过于复杂。即使如此,出于完整性考虑:
class TableViewController: UITableViewController {
enum TrainingSection {
case currentTraining(FTCurrentTraining)
case qrCode
case trainingMethod
}

var sections: [TrainingSection] = [.qrCode, .trainingMethod]

func updateSections() {
// When some kind of state changes, rebuild `sections` to include the relevant sections
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch sections[indexPath.section] {
case .currentTraining(let training):
return currentTrainingCell(tableView, indexPath, currentTraining: currentTraining)
case .qrCode:
return qrCodeCell(tableView, indexPath)
case .trainingMethod:
return trainingMethodCell(tableView, indexPath)
default:
fatalError("Unknown section")
}
}
}

关于ios - 如何在 TableView 部分使用一个带有可选用例的枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59443937/

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