ios - Swift 结构体返回成员

标签 ios swift swift4

我有一个类似的结构:

struct SectionInfo {
    enum AccountSectionInfo: Int, CaseIterable {
        case accountCreationDate
        case referredBy
        case referralCount
    }
    enum StatisticsSectionInfo: Int, CaseIterable {
        case tasksCompleted
             case pointsScored
    }
  // return AccountSectionInfo for index 0, for 1 return StatisticsSectionInfo
  func section(forIndex index: Int) -> XXX {}
}

我想在 TableView 中使用它,如下所示:

cellForRow(at: IndexPath) -> UITableViewCell {
    let section = SectionInfo.section(at: indexPath.section)
    if section == AccountInfoSection.rawValue {

    }
}

这在 Swift 中可能吗?

最佳答案

basically I want to have an index (indexPath.section) and I want to get a certain member (enum) of the structure based on the index (the rows enum)

那么您的结构旨在作为 TableView 数据源的模型?那么这是一个非常糟糕的模型。自然的方式是这样的:

struct Row {
    // properties needed to populate a row
}
struct Section {
    let title : String // or whatever else you need to populate section header
    var rows : [Row]
}
var model : [Section]

现在您可以获得一行model[indexPath.section].rows[indexPath.row]

关于ios - Swift 结构体返回成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51449375/

相关文章:

swift - 将 UnsafeMutableRawPointer 转换为 UInt64

ios - 创建 NSIndexPath 后如何设置它

ios - 在下一个 View 中编辑标签很长

swift - 在 Swift 中将 Bearer Token 添加到 header 时出现问题(HTTP POST 调用)

ios - DFT 结果使用来自 Swift 的 vDSP 来自实值输入(Surge 实现)

Swift (IOS) 更新异步函数中的参数

ios - 自定义 Segue,多个动画不同步

iphone - 野外的 NSOperationQueueDefaultMaxConcurrentOperationCount

ios - 如何让手电筒闪烁?

ios - 如何创建多 ImageView 设计?