首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使IndexPath rawRepresentable?

如何使IndexPath rawRepresentable?
EN

Stack Overflow用户
提问于 2019-06-13 12:14:40
回答 2查看 315关注 0票数 2

我正在尝试重构代码并创建一个enum类来保存表视图单元格的indexPath。

我想让代码以这样的方式工作:

代码语言:javascript
复制
enum TableViewCell: IndexPath {
     case shopImageView = [0,0]
     case selectShopImageButton = [0,1]
}

但是编译器说indexPath不是rawRepresentable:

“TableViewCell”声明原始类型“IndexPath”,但不符合RawRepresentable,无法合成一致性 枚举大小写的原始值必须是文字。

我怎样才能使indexPath rawRepresentable?目前的代码是这样工作的,我想对其进行改进。

代码语言:javascript
复制
enum TableViewCell {
    case shopImageView
    case selectShopImageButton
    case shopNameLocal
    case shopNameEN
    case addressLocal
    case addressEN
    case selectAddressButton
    case openingHours
    case datePickers
    case phone
    case email
    case uploadShopFormButton
    
    var indexPath: IndexPath {
        switch self {
        case .shopImageView:         return [0,0]
        case .selectShopImageButton: return [0,1]
        case .shopNameLocal:         return [0,2]
        case .shopNameEN:            return [0,3]
        case .addressLocal:          return [0,4]
        case .addressEN:             return [0,5]
        case .selectAddressButton:   return [0,6]
        case .openingHours:          return [0,7]
        case .datePickers:           return [0,8]
        case .phone:                 return [0,9]
        case .email:                 return [0,10]
        case .uploadShopFormButton:  return [0,11]
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-01 13:01:03

使用硬编码的TableView IndexPath的另一种方法是使用具有静态属性的enum。这样做的好处是,如果在各种TableView委托方法中引用IndexPath,并且需要更改IndexPath引用,则只需在枚举中更改它们一次。

代码语言:javascript
复制
 private enum TableIndex {
    // Section 0: Information
    static let information =        IndexPath(row: 0, section: 0)
    // Section 1: Preferences
    static let notifications =      IndexPath(row: 0, section: 1)
    static let units =              IndexPath(row: 1, section: 1)
    static let hapticFeedback =     IndexPath(row: 2, section: 1)
    // Section 2: Links
    static let leaveReview =        IndexPath(row: 0, section: 2)
    static let support =            IndexPath(row: 1, section: 2)
    static let privacyPolicy =      IndexPath(row: 2, section: 2)
    static let disclaimer =         IndexPath(row: 3, section: 2)
}

然后你可以这样引用它们:

代码语言:javascript
复制
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch indexPath {
    case TableIndex.information:
        // do something
    case TableIndex.notifications:
        // do something
    case TableIndex.units:
        // do something

        // etc

    default:
        break
    }
}
票数 3
EN

Stack Overflow用户

发布于 2019-06-13 12:30:35

还有其他方法,但不一定更好,无论如何,您可以将代码缩减到此。

代码语言:javascript
复制
enum TableViewCell: Int  {
    case shopImageView = 0
    case selectShopImageButton = 1
    case shopNameLocal = 2
    case shopNameEN = 3
    case addressLocal
    case addressEN
    case selectAddressButton
    case openingHours
    case datePickers
    case phone
    case email
    case uploadShopFormButton
    
    var indexPath: IndexPath {
        return [0, self.rawValue]
    }
}

请记住,这也取决于您如何使用它们,比如在何处以及如何传递参数。

其中一种方法。

代码语言:javascript
复制
extension IndexPath {
     init(using type: TableViewCell) {
        self.init(row: type.rawValue, section: 0)
    }
}

let indexPath = IndexPath(using: .shopNameEN)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56580271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档