Giter Club home page Giter Club logo

owlcollectionview's Introduction

OwlCollectionView

This is a version for CoreData. If you are using Realm, take a look into this version: https://github.com/thanhtanh/OwlRealmCollectionView

Are you tired when you want to make data be synced between viewcontrollers? Such as you want:

  • When you add an item, it will be added into list immediately
  • When you delete/edit an item from CoreData, it will be deleted/edited in list
  • When you change the value of one item, and want to reorder the items in list, it also do that for you, immediately

This library is written based on NSFetchedResultsController, to listen the data changed event from CoreData, then update data to UI for you. You will not have to implement NSFetchedResultsControllerDelegate by youself for every viewcontroller.

How to use

Configuration:

What you need is two classes OwlCVDataSource and OwlCVDelegate. So, copy them to anywhere in your project. Because this library use MagicalRecord, so you should have MagicalRecord in your Podfile.

Implementation

When you want to implement a UICollectionView in your project, you usually do steps like below:

  • Add a UICollectionView to your storyboard. Assume that I name it collectionView
  • Add a cell to your collection view.
  • Prepare datasource data for you collection view
  • Make your viewcontroller conform UICollectionViewDelegate, UICollectionViewDataSource

With OwlCollectionView, you just need to change some small things:

  • Create collection view data source with OwlCVDataSource

      let dataSource = OwlCVDataSource.datasourceConfig_dataClass(dataModelClass: Owl.self, predicate: NSPredicate.init(value: true), sortBy: "height", sortAscending: true, groupBy: nil)
      // `Owl` is the model for entity in CoreData
    
  • Create a subclass of OwlCVDelegate and implement any method of UICollectionViewDelegate and UICollectionViewDataSource as the normal way

      class OwlDel: OwlCVDelegate {
          var vc: ViewController!
    
          class func delegateForCV(grid:UICollectionView,
              withVC vc:ViewController,
              datasource:OwlCVDataSource) -> OwlDel {
              let delegate = OwlDel()
    
              delegate.vc = vc
              delegate.collectionView = grid
    
              grid.delegate = delegate
              grid.dataSource = delegate
    
              delegate.dataSource = datasource
    
              return delegate
          }
    
          override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
              let obj = self.dataObjectFor(item: nil, at: indexPath) as! Owl
    
              ....
              return item
          }
    
          override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
              return CGSize(width: 200, height: 50)
          }
      }
    
  • Initial an delegate object for your collection view

      var del: OwlDel! // class variable
      // init with the dataSource we create above
      // `grid` is the collectionview we want to use
      self.del = OwlDel.delegateForCV(grid: self.grid, withVC:self, datasource:dataSource)
    
  • Reload collectionview

      self.grid.reloadData()
    

Example

Please take a look on my attached example to more clean what I said above. Screenshot:

screenshot

Explain the screenshot:

  • Requirement: A timer will add an owl after each 2 seconds with random height (the number at the end of each item). If the new owl with the height already existed in the list, it just been updated info (a random color) only. If height = 2, remove that existed owl. All owls in the list will be sorted by height.
  • Solve: At you can see the example in project, I just add/update/delete new owl to CoreData, and don't care how UI update. It also update when you a view controller is not displayed to user.

License

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

owlcollectionview's People

Contributors

thanhtanh avatar

Stargazers

 avatar Tri Tran avatar Ngo Huu Tuan avatar  avatar Tran Trung Hieu avatar Phan Hoàng Quốc An avatar Hai Tran avatar Ky Nguyen avatar  avatar

Watchers

James Cloos avatar  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.