UITableView 协议方法中更好的 switch 使用方案

UITableView 协议方法中更好的 switch 使用方案,第1张

概述UITableView 几乎是每一个原生 App 都会使用到的控件,所以你一定对 UITableViewDataSource、UITableViewDelegate 中的代理方法很熟悉。在一些代理方法中有时候我们需要对不同的 section、row 进行分开处理,此时我们都会使用 switch 来应对。所以接下来将会介绍一种更好的 switch 使用方案。 常规处理 大多数情况下,我们使用的 sw

UItableVIEw 几乎是每一个原生 App 都会使用到的控件,所以你一定对 UItableVIEwDataSourceUItableVIEwDelegate 中的代理方法很熟悉。在一些代理方法中有时候我们需要对不同的 section、row 进行分开处理,此时我们都会使用 switch 来应对。所以接下来将会介绍一种更好的 switch 使用方案。

常规处理

大多数情况下,我们使用的 switch 方案都是类似下面的硬编码方式:

func tableVIEw(_ tableVIEw: UItableVIEw,cellForRowAt indexPath: IndexPath) -> UItableVIEwCell {    switch indexPath.section {        case 0: // Dequeue a cell for section 0        case 1: // Dequeue a cell for section 1        // And so on...        default: fatalError("Unhandled section \(indexPath.section)")    }}

上诉代码在功能上并没有什么权限,但是这种硬编码方式对后期的代码维护带来的隐性成本。其实一个好的编程习惯就是最大限度的避免硬编码,无论是 switch 还是设置 vIEw 的 tag 的时候都应该采用更为“优雅”的方式。

更好的处理方式

对于硬编码的优雅的替换方案大多数情况下都是使用 enum 方式,在枚举类型中我们可以使用更具识别性的词语。这样不仅能一眼识别含义,而且在后期修改的过程中也不用满世界找硬编码的数字生怕有遗漏。

接下来直接上码:

protocol ReusableVIEwEnum {}extension ReusableVIEwEnum where Self: RawRepresentable,Self.RawValue == Int {    static var all: [Self] {        var index = 0        var allitems = [Self]()        while let item = Self(rawValue: index) {            allitems.append(item)            index += 1        }        return allitems    }    static func build(with value: Int) -> Self {        guard let row = Self(rawValue: value) else {            fatalError("Unimplemented value: \(value)")        }        return row    }}// ---------------------// EXAMPLE USAGE CODE:// You only have to define the enumfileprivate enum ProfileSections: Int,ReusableVIEwEnum {    case picture = 0    case name    case email    case password}// And use it in your code:func numberOfSections(in tableVIEw: UItableVIEw) -> Int {    return ProfileSections.all.count}func tableVIEw(_ tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int {    switch ProfileSection.build(with: section) {        case .picture   : return 1        case .name      : return 1          case .email     : return 1         case .password  : return 1     }}   func tableVIEw(_ tableVIEw: UItableVIEw,cellForRowAt indexPath: IndexPath) -> UItableVIEwCell {    switch ProfileSection.build(with: indexPath.section) {        case .picture   : // Dequeue profile picture section        case .name      : // Dequeue name section        case .email     : // Dequeue email section        case .password  : // Dequeue password section    }}

首先,我们什么了 ReusableVIEwEnum 协议并对其进行了拓展。在拓展中我们添加了一个计算属性 all, 用于表示遵循该协议并且 rawValue 为 Int的枚举类型的所有取值。另外增加了用 Int 来构建枚举类型的函数 build

在使用实例中我们定义了枚举类型 ProfileSections,并在 UItableVIEw 的代理方法中实现了上面 ReusableVIEwEnum 协议的应用。应用后的 switch 方案代码风格更为有效,后期维护效率也明显优于之前的硬编码方式。

总结

以上是内存溢出为你收集整理的UITableView 协议方法中更好的 switch 使用方案全部内容,希望文章能够帮你解决UITableView 协议方法中更好的 switch 使用方案所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1064386.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存