Cellmodel-driven collectionview manager

Overview

Sapporo

Language CocoaPods [Carthage compatible] (https://github.com/Carthage/Carthage) License Issues

cellmodel-driven collectionview manager

Features

  • Easy to manage your sections and cells (reset/insert/append/remove/update)
  • Don't have to write the code for UICollectionViewDelegate and UICollectionViewDataSource protocols
  • Don't need to care about cell identifier
  • Handle cell selection by trailing closure
  • Supports method chaining
  • Supports subscript
  • Complete example
Quick example
// viewController swift file

let sapporo = Sapporo(collectionView: self.collectionView)

let cellmodel = YourCellModel(title: "Title", des: "description") {
	println("Did select cell with title = \(title)")
}

let topSection = SASection()

sapporo
	.reset(topSection)
	.bump()

topSection
	.append(cellmodel)	// append a new cell model in datasource
	.bump()	// show the new cell in the collection view

topSection
	.remove(1...3)
	.bump()

topSection
	.move(fromIndex: 0, toIndex: 3)
	.bump()
// your cell swift file

class YourCellModel : SACellModel {
	let title: String
	let des: String

	init(title: String, des: String, selectionHandler: (SACell) -> Void) {
		self.title = title
		self.des = des
		super.init(cellType: YourCell.self, selectionHandler: selectionHandler)
	}
}

class YourCell : SACell, SACellType {
	typealias CellModel = YourCellModel

	@IBOutlet weak var titleLabel: UILabel!

	override func configure() {
		super.configure()

		guard let cellmodel = cellmodel else {
			return
		}

		titleLabel.text = cellmodel.title
      	}
	}

Usage

  • Handling section
// retrieve a section or create a new section if it doesn't already exist
let section = sapporo[0]
	
// inserting
sapporo.insert(section, atIndex: 1)
	 .bump()

// moving
sapporo.move(fromIndex: 1, toIndex: 5)
	.bump()

// removing
sapporo.remove(index)
	.bump()

// remove all data
sapporo.reset()
	.bump()

// handing section index by enum
enum Section: Int, SASectionIndexType {
	case top
	case center
	case bottom

	static let count = 3
}

let topSection = sapporo[Section.Top]
  • Handling cell
// appending
sapporo[0]
	.append(cellmodel)	// append a cellmodel
	.bump()	// and bump to show the cell in the collection view

sapporo[TopSection]
	.append(cellmodels)	// append a list of cellmodels
	.bump()					

// by using section
let section = sapporo[Section.Top]
section
	.append(cellmodel)
	.bump()

// 2. inserting
section
	.insert(cellmodels, atIndex: 1)
	.bump()

section
	.insertBeforeLast(cellmodels)
	.bump()

// 3. reseting
section
	.reset(cellmodels)	// replace current data in section by the new data
	.bump()

section
	.reset()	// or remove all data in section
	.bump()

// 4. moving
section
	.move(fromIndex: 5, toIndex: 1)
	.bump()

// 5. removing
section
	.remove(1)
	.bump()

section
	.remove(cellmodel)
	.bump()

section
	.remove(2...5)
	.bump()

section
	.removeLast()
	.bump()

// updating cell
let cellmodel = section[1]
cellmodel.property = newData
cellmodel.bump()

// able to retrieve a cellmodel by indexpath
let cellmodel = sapporo[indexpath]
  • Registering cell, header, footer, reusable view
sapporo
	.registerCellByNib(CustomCell)
	.registerCell(SimpleCell)
	.registerSupplementaryViewByNib(HeaderView.self, kind: "SectionHeader")
  • Customizing layout

In case you want to customize the layout of collection view, just create a subclass of SALayout and call setLayout method to set the new layout instance.

class CustomLayout: SALayout {
	// the implementation for your layout
}

let layout = CustomLayout()
sapporo.setLayout(layout)

Installation

  • Using Carthage

    • Insert github nghialv/Sapporo to your Cartfile
    • Run carthage update
  • Using CocoaPods

    • Insert followings to your Podfile
     use_frameworks!
    
     target 'YOUR_TARGET_NAME' do
       pod 'Sapporo'
     end
    
    • Run pod install
  • Using submodule

Requirements

  • iOS 9.0+
  • Xcode 9+
  • Swift 4

License

Sapporo is released under the MIT License.

Comments
  • Xcode 7.0

    Xcode 7.0

    Will you be updating this to Xcode 7.0, please? It currently will not build for me. Thanks. Edit: I'm getting init() is unavailable for UICollectionViewTransitionLayout().

    opened by allyrgb 6
  • Swift3 version cannot be used by cocoapods.

    Swift3 version cannot be used by cocoapods.

    Would you please check whether Sapporo swift3 version can be imported by Cocoapods? When I follow the instruction for installation using Cocoapods, the old swift2 version is imported. Thank you for your nice library.

    opened by frmh0313 1
  • important question

    important question

    viewForSupplementaryElementOfKind in collection view datasource function but it is never be called i want to know why

    this is my source override func viewDidLoad() { super.viewDidLoad() mCollectView.backgroundColor = UIColor.whiteColor() mCollectView.dataSource = self mCollectView.delegate = self collectLayout.headerReferenceSize = CGSizeMake(mCollectView.frame.width, 100) mCollectView.registerClass(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cellID") mCollectView.registerClass(YjiSectionHeader.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "MyHeader") mCollectView.collectionViewLayout = collectLayout }

    @IBAction func changeMode(sender: AnyObject) {
        mCollectView.setCollectionViewLayout(collectLayout2, animated: true)
    }
    
    // section number
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 2
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cellID = "cellID"
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellID, forIndexPath: indexPath)
        cell.backgroundColor = UIColor.yellowColor()
    
        return cell
    }
    
    
    // ヘッダーを表示する
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        var header : YjiSectionHeader? = nil
        if (kind == UICollectionElementKindSectionHeader) {
            header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "MyHeader", forIndexPath: indexPath) as? YjiSectionHeader
            if header == nil {
                header = YjiSectionHeader(frame: CGRectMake(0, 0, collectionView.frame.width, 50))
    
            }
        }
        return header!
    }
    // ヘッダーのサイズを設定する
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: 100)
    }
    
    opened by yunyun1989114 0
  • Question on the Calendar view

    Question on the Calendar view

    Hi

    Thanks for this project which is very useful. However I would have a question regarding the handling of the vertical "Hour " as I don't undestand where it is managed . I tried to chnage the number of hours per day with no effect. From where it is dequeued ? Hope it's clear :) extension CalendarViewController: SapporoDelegate { func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { if kind == CalendarHeaderType.Day.rawValue { let view = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: CalendarHeaderView.reuseIdentifier, forIndexPath: indexPath) as! CalendarHeaderView view.titleLabel.text = "Day (indexPath.item + 1)" return view }

    **    let view = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: CalendarHeaderView.reuseIdentifier, forIndexPath: indexPath) as! CalendarHeaderView
        view.titleLabel.text = "Hour \(indexPath.item + 1)"
        return view**
    
    opened by roro95 0
  • NSInternalInconsistencyException

    NSInternalInconsistencyException

    Hello, i really can't find the reason why this error is raised since everything is in line with your examples. I have never worked with CollectionView before, so maybe i'm missing something fundamental? Maybe it's because i try to implement this into a widget?

    @IBOutlet weak var viewCollection: UICollectionView!
    
    lazy var sapporo: Sapporo = { [unowned self] in
        return Sapporo(collectionView: self.viewCollection)
        }()
    

    Then in viewDidLoad:

    var models : [CollectionCellModel] = [];
    
        for participation in participations {
    
            let model = CollectionCellModel(title: participation.module.name, des: "", selectionHandler: {(cell) -> Void in
                println(cell.cellmodel)
            })
    
            models.append(model);
        }
    
        sapporo[0].insert(models, atIndex: 1).bump()
    

    leads to:

        Widget[71046:859777] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0.  The number of items contained in an existing section after the update (2) must be equal to the number of items contained in that section before the update (2), plus or minus the number of items inserted or deleted from that section (2 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'
    *** First throw call stack:
    (
        0   CoreFoundation                      0x000000010a85bc65 __exceptionPreprocess + 165
        1   libobjc.A.dylib                     0x000000010c409bb7 objc_exception_throw + 45
        2   CoreFoundation                      0x000000010a85baca +[NSException raise:format:arguments:] + 106
        3   Foundation                          0x000000010acf898f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
        4   UIKit                               0x000000010b72a78e -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:] + 12818
        5   Sapporo                             0x000000010c6438b3 _TFC7Sapporo7Sapporo6bumpMefS0_FOS_15SectionBumpTypeT_ + 899
        6   Sapporo                             0x000000010c64403e _TTWC7Sapporo7SapporoS_17SASectionDelegateS_FS1_6bumpMeUS1___fQPS1_FOS_15SectionBumpTypeT_ + 14
        7   Sapporo                             0x000000010c637427 _TFC7Sapporo9SASection4bumpfS0_FT_T_ + 759
    
    opened by eisfair 0
Releases(3.0.0)
Owner
Le Van Nghia
Software Engineer. Lover of OSS. Creator of PipeCD, Promviz, LotusLoad, Hakuba, MaterialKit... 🐳
Le Van Nghia
Easy way to integrate pagination with dummy views in CollectionView, make Instagram "Discover" within minutes.

AZCollectionView Controller Features Automatic pagination handling No more awkward empty CollectionView screen AZ CollectionVIew controller give you a

Afroz Zaheer 95 May 11, 2022
CollectionView - UICollectionView using UICollectionViewCompositionalLayout

CollectionView UICollectionView using UICollectionViewCompositionalLayout create

null 0 Jan 11, 2022
Custom CollectionViewLayout class for CollectionView paging mode to work properly

PagingCollectionViewLayout About How to use About ⚠️ Custom class, which is inherited from UICollectionViewFlowLayout, developed for properly work Col

Vladislav 2 Jan 17, 2022
A data-driven UICollectionView framework for building fast and flexible lists.

A data-driven UICollectionView framework for building fast and flexible lists. Main Features ?? Never call performBatchUpdates(_:, completion:) or rel

Instagram 12.5k Jan 1, 2023
Cellmodel-driven tableview manager

Cellmodel-driven tableview manager

Le Van Nghia 476 Nov 6, 2022
💾 🔜📱 Type-safe data-driven CollectionView, TableView Framework. (We can also use ASCollectionNode)

⚠️ The latest updates is this PR. It changes the difference algorithm to DifferenceKit. DataSources ?? ?? ?? Type-safe data-driven List-UI Framework.

Muukii 563 Dec 16, 2022
A dependency manager driven by SwiftPM that works for iOS/tvOS/watchOS/macOS projects.

Installation • Usage • Supporting Accio • Contributing • License ⚠️ Deprecation Notice ⚠️ With the release of Xcode 12 which includes Swift 5.3, we fe

Jamit Labs 647 Dec 25, 2022
Easy way to integrate pagination with dummy views in CollectionView, make Instagram "Discover" within minutes.

AZCollectionView Controller Features Automatic pagination handling No more awkward empty CollectionView screen AZ CollectionVIew controller give you a

Afroz Zaheer 95 May 11, 2022
A CollectionView Layout displaying a slanted cells

CollectionViewSlantedLayout is a subclass of the UICollectionViewLayout allowing the display of slanted cells in a UICollectionView. Features Pure Swi

Yassir Barchi 2.2k Dec 27, 2022
Fancy news app that diwnloads data from new.com api and shows it in a paging collectionview.

Fancy news reader PAging collection view that shows latest news from techcurch.com and displays full content in a detail page, was built to practice t

Abdul-Mujeeb Aliu 31 Feb 10, 2022
a collectionView made for Sprite Kit

CollectionNode A collectionView made for Sprite Kit installation Carthage Carthage is a dependency manager that provides binary frameworks for your pr

Bruno Wide 104 Sep 21, 2022
Aplicativo simples demonstrando como implementar ContextMenu em uma CollectionView

Aplicativo simples demonstrando como implementar ContextMenu em uma CollectionView Ambiente de desenvolvimento Estas são as versões das ferramentas qu

Junior Silva 0 Nov 26, 2021
Cusom CollectionView card layout

MMCardView Example To run the example project, clone the repo, and run pod install from the Example directory first. Demo 1.Card Requirements iOS 8.0+

Millman Yang 556 Dec 5, 2022
:star: Custom card-designed CollectionView layout

CardsLayout CardsLayout is a lightweight Collection Layout. Installation CocoaPods You can use CocoaPods to install CardsLayout by adding it to your P

Filipp Fediakov 798 Dec 28, 2022
Easy way to integrate pagination with dummy views in CollectionView, make Instagram "Discover" within minutes.

AZCollectionView Controller Features Automatic pagination handling No more awkward empty CollectionView screen AZ CollectionVIew controller give you a

Afroz Zaheer 95 May 11, 2022
A CollectionView Layout displaying a slanted cells

CollectionViewSlantedLayout is a subclass of the UICollectionViewLayout allowing the display of slanted cells in a UICollectionView. Features Pure Swi

Yassir Barchi 2.2k Dec 27, 2022
CollectionView - UICollectionView using UICollectionViewCompositionalLayout

CollectionView UICollectionView using UICollectionViewCompositionalLayout create

null 0 Jan 11, 2022
Custom CollectionViewLayout class for CollectionView paging mode to work properly

PagingCollectionViewLayout About How to use About ⚠️ Custom class, which is inherited from UICollectionViewFlowLayout, developed for properly work Col

Vladislav 2 Jan 17, 2022
Task-Manager - Task Manager App With Swift

Task-Manager It's typical task manager where user can assign the importance, def

Andrey Buchevskiy 1 Jan 10, 2022
Ease is an event driven animation system that combines the observer pattern with custom spring animations as observers

Ease is an event driven animation system that combines the observer pattern with custom spring animations as observers. It's magic. Features Animate a

Robert-Hein Hooijmans 1.3k Nov 17, 2022