我有一个类似的结构:
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/