YLExtensions 2.0.0

YLExtensions 2.0.0

YuLeiFuYun 维护。



  • YuLeiFuYun

YLExtensions

使用 YLEXtensions 可以在注册、配置一个包含多种类型单元格的 UITableView 或 UICollectionView 页面时避免大量的模板代码。

要求

  • iOS 13.0+
  • Swift 5.1+

安装

Cocoapods

要使用 Cocoapods 将 YLExtensions 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '13.0'
use_frameworks!

target 'MyApp' do
  # your other pod
  # ...
  pod 'YLExtensions'
end

运行 pod install 以构建您的依赖。

Swift包管理器

选择文件 > Swift包 > 添加包依赖。在“选择包存储库”对话框中输入 https://github.com/YuLeiFuYun/YLExtensions.git

用法

在 SomeModel.swift 中

import YLExtensions

// Let SomeModel adopts and conforms to the ModelType protocol
struct SomeModel: ModelType {
    let someA: [A]
    let someB: [B]
    let someC: [C]
    let someD: [D]
    
    var data: [[Any]] {
        return [someA, someB, someC, someD]
    }
}

extension SomeModel {
    static var tCells: [UITableViewCell.Type]? {
        // All cell types created by pure code.
        [ACell.self, BCell.self]
    }
    
    static var tNibs: [UITableViewCell.Type]? {
        // All cell types created by nib.
        [CCell.self, DCell.self]
    }
    
    static var tAll: [UITableViewCell.Type]? {
        // All cell types, Sort by display order.
        [ACell.self, BCell.self, CCell.self, DCell.self]
    }
}

在 SomeCell.swift 中

class SomeCell: UITableViewCell {
    ...
    // Configure cell
    override func configure(_ model: Any?) {
        ...
    }
}

在 SomeViewController.swift 中

import YLExtensions

// 1. Create a model object
let someModel = SomeModel(...)

// 2. Register cells
override func viewDidLoad() {
    super.viewDidLoad()
    ...
    tableView.registerCells(with: SomeModel.tCells!)
    tableView.registerNibs(with: SomeModel.tNibs!)
}

// 3. Create and configure cells
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // Note: Only applies when cells of the same type are all together and the different types of cells are in different section.
    let cell = tableView.dequeueReusableCell(for: indexPath, with: SomeModel.tAll!)
    cell.configure(someModel.data[indexPath.section][indexPath.row])
    return cell
}

许可

YLExtensions 在MIT许可下发布。有关详细信息,请参阅LICENSE。