Giter Club home page Giter Club logo

rxdelegatecells's Introduction

RxDelegateCells

Swift CocoaPods Carthage compatible

UITableView Cell Height and UICollectionView Cell Size for RxSwift.

Why

If you're using RxSwift with UITableView or UICollectionView, it is difficult to calculate the cell sizes with Observables. Because UITableView requires cell height synchronously, but Observables emit values asynchronously.

For example, assume that we are using MVVM pattern and we have a ViewModel which exposes a message with Driver. We need an actual message value as a String to calculate the cell height. However, since message emits values asynchronously, we cannot access the actual value synchronously.

struct MyCellModel {
    let message: Driver<String>
}

func tableView(tableView: UITableView, heightForRowAtIndexPath: NSIndexPath) -> CGFloat {
    let cellViewModel = self.dataSource.itemAtIndexPath(indexPath)

    cellModel.message // (1) This is `Driver<String>`
        .map { message -> CGFloat in
            let size = CGSize(width: tableView.width - 20, height: .max)
            let options: NSStringDrawingOptions = [.UsesLineFragmentOrigin, .UsesFontLeading]
            let attributes = [NSFontAttributeName: MyCell.Font.messageLabel]
            let rect = message.boundingRectWithSize(size, options: options, attributes: attributes, context: nil)
            return ceil(rect.height) + 20 // (2) We want return this value (async)
        }

    return 40 // (3) But we have to return actual value here, synchronously :(
}

At a Glance

RxDelegateCells makes it available to use Driver<CGFloat> as a UITableView cell height and Driver<CGSize> as a UICollectionView cell size.

This code makes the cell heights 40, 80 and 120.

Driver.just([[.just(40), .just(80), .just(120)]])
    .drive(self.tableView.rx_cellHeightWithDelegate(delegate))
    .addDisposableTo(self.disposeBag)

rxdelegatecells

You can find a real-world example at here.

Installation

  • For iOS 8+ projects with CocoaPods:

    pod 'RxDelegateCells', '~> 0.1'
  • For iOS 8+ projects with Carthage:

    github "devxoul/RxDelegateCells" ~> 0.1
    
  • For iOS 7 projects with CocoaSeeds:

    github 'devxoul/RxDelegateCells', '0.1.0', :files => 'Sources/**/*.swift'
  • Using Swift Package Manager:

    import PackageDescription
    
    let package = Package(
        name: "MyAwesomeApp",
        dependencies: [
            .Package(url: "https://github.com/devxoul/RxDelegateCells", "0.1.0"),
        ]
    )

License

RxDelegateCells is under MIT license. See the LICENSE file for more info.

rxdelegatecells's People

Contributors

devxoul avatar

Watchers

Carabineiro 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.