UITableView 几乎是每个原生 App 都会使用到的控件,因此你必定对 UITableViewDataSource、UITableViewDelegate 中的代理方法很熟悉。在一些代理方法中有时候咱们须要对不一样的 section、row 进行分开处理,此时咱们都会使用 switch 来应对。因此接下来将会介绍一种更好的 switch 使用方案。编程
大多数状况下,咱们使用的 switch 方案都是相似下面的硬编码方式:app
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 方式,在枚举类型中咱们可使用更具识别性的词语。这样不只能一眼识别含义,并且在后期修改的过程当中也不用满世界找硬编码的数字生怕有遗漏。ui
接下来直接上码:编码
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 enum fileprivate 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。spa
在使用实例中咱们定义了枚举类型 ProfileSections,并在 UITableView 的代理方法中实现了上面 ReusableViewEnum 协议的应用。应用后的 switch 方案代码风格更为有效,后期维护效率也明显优于以前的硬编码方式。代理