Code Monkey home page Code Monkey logo

ylextensions's Introduction

YLExtensions

Using YLEXtensions could get you out of massive boilerplate code when you want to register, configure a UITableView or UICollectionView page which contains multiple types of cells.

Requirements

  • iOS 13.0+
  • Swift 5.1+

Installation

Cocoapods

To integrate YLExtensions into your Xcode project using CocoaPods, specify it in your Podfile:

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

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

Run pod install to build your dependencies.

Swift Package Manager

Select File > Swift Packages > Add Package Dependency. Enter https://github.com/YuLeiFuYun/YLExtensions.git in the "Choose Package Repository" dialog.

Usage

In SomeModel.swift:

import YLExtensions

// Let SomeModel adopts and conforms to the ModelType protocol
struct SomeModel: ModelType {
    typealias Value = Never
    
    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]
    }
}

In SomeCell.swift:

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

In 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
}

License

YLExtensions is released under the MIT license. See LICENSE for details.

ylextensions's People

Contributors

yuleifuyun avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.