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

标签 ios swift generics

我有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/

相关文章:

ios - 在 TableView 单元格中显示来自 Firebase 的数据

ios - swift :我 super 困惑! UILabel 上的 UIGravityBehavior vs NSTimer vs CGPointMake

ios - 如何创建具有权利的新配置文件?

ios - 为什么 TabBar 在 segue 之后隐藏?

c# - 如何实现一个类的泛型和非泛型版本?

ios - 按钮未注册水龙头

swift - 懒惰的内部引用

ios - 在不知道其索引的情况下选择 UITabBarController 中的特定 viewController

vb.net - 如何使用表单中的属性网格来编辑任何类型

java - 如何避免泛型警告