Giter Club home page Giter Club logo

azcollectionviewcontroller's Introduction

AZCollectionView Controller

Awesome Swift version Support Dependecy Manager Version License Platform

Features

  • Automatic pagination handling
  • No more awkward empty CollectionView screen
  • AZ CollectionVIew controller give you advantage to connect your (Loading, no result, error ) views via Interface builder
  • You can also add your custom xib as dummy views (loading, error, no result)

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate AZ CollectionVIew controller into your Xcode project using CocoaPods, specify it in your Podfile:

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

target '<Your Target Name>' do
pod 'AZCollectionViewController'
end

Then, run the following command:

$ pod install

Usage

Step 1

  • Extend your view controller from AZCollectionViewController
class ViewController: AZCollectionViewController {

    var lastIndex = 0
    var results = [String]()

    override func viewDidLoad() {
    
        nextLoadingCellNibName = "LoadingCollectionViewCell"
        nextLoadingCellIdentifier = "LoadingCollectionViewCell"
        loadNextPageLoaderCell(nibName: nextLoadingCellNibName, cellIdentifier: nextLoadingCellIdentifier) // Only if you want personal NextPage loading cell
        
        super.viewDidLoad()
        self.fetchData()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
}

Step 2

  • Same as AZTableVIewController
  • Set the loading views outlet as given below
  • Connect collectionView outlet as well

Alt text

Alt text

  • To load views from custom .xib files
class ViewController: AZCollectionViewController {
    override func viewDidLoad() {

        self.loadLoadingView(nibName: "your nib name") // if bundle is nil
        self.loadErrorView(nibName: "your nib name", bundle: yourBundle) // if custom bundle

        super.viewDidLoad()
        self.fetchData()
    }
}
  • If your xibs are in main bundle than use
    self.loadLoadingView(nibName: "your nib name") // if bundle is nil

Else use

    self.loadLoadingView(nibName: "your nib name", bundle: yourBundle)

Step 3

  • And override AZCollectionView cellForRow function.
extension ViewController {

    override func AZCollectionView(_ collectionView: UICollectionView, cellForRowAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TestCollectionViewCell
        
        cell.feedImage.image = UIImage(named: self.results[indexPath.row])
        
        return cell
    }
    
    override func AZCollectionView(_ collectionView: UICollectionView, heightForRowAt indexPath: IndexPath) -> CGSize {
    
        return CGSize(width: (collectionView.frame.size.width / 3) - 6, height: (collectionView.frame.size.width / 3) - 4)
        
    }
}

Step 4

  • Override two more functions "fetchData" and "fetchNextData"
//MARK: - API Call
extension ViewController {
    
    override func fetchData() {
        super.fetchData()
        
        FakeService.getData { (error, results) in
           
            if let resu = results {
                self.results.removeAll()
                self.results.append(contentsOf: resu)
                self.didfetchData(resultCount: resu.count, haveMoreData: true)
            }
                
            else if let error = error {
                self.errorDidOccured(error: error)
            }
        }
    }
    
    override func fetchNextData() {
        super.fetchNextData()
        
        FakeService.getData (offset: results.count) { (error, results) in
            
            if let resu = results {
                
                self.results.append(contentsOf: resu)
                if self.results.count < 45 { // you probably get next page exist from service.
                    self.didfetchData(resultCount: resu.count, haveMoreData: true)
                }
                else {
                    self.didfetchData(resultCount: resu.count, haveMoreData: false)
                }
            }
            else if let error = error {
                self.errorDidOccured(error: error)
            }
        }
    }
}

Done

Thats it, you successfully integrate AZCollectionViewController

License

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

Author

Afroz Zaheer - (https://github.com/AfrozZaheer)

azcollectionviewcontroller's People

Contributors

afrozzaheer avatar azharashiq178 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

azcollectionviewcontroller's Issues

viewForSupplementaryElementOfKind

Is it possible to add custom header in collection view ? Like with standard functions:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {)

Subviews misaligned

Why is it that all subviews, such as loading, no results, etc. are slightly offset to the right? This can also be seen in the example project, and happens with every method of loading those views - I've tried using accessory views in IB, setting a custom XIB class, or using the predefined ones and all of them are exhibiting the same problem.

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.