Cellmodel-driven tableview manager

Related tags

Layout Hakuba
Overview

Hakuba

Platform Language License Issues

I want to slim down view controllers.

I want to manage tableview without the code of UITableViewDelegate and UITableViewDataSource.

That is why I created Hakuba.

( Hakuba is one of the most famous ski resorts in Japan. )

Features

  • Don't have to write the code for UITableViewDelegate and UITableViewDataSource protocols
  • Easy to manage your sections and cells (append/reset/insert/remove/update)
  • Support dynamic cell height from ios7
  • Don't have to worry about cell identifier
  • Handling cell selection by trailing closure
  • Easy to implement header/footer view (floating callback)
  • Support for creating cells from Nibs or Storyboards
  • Method chaining
  • Subscript
  • Support loadmore closure
  • Complete example
Quick example
// viewController swift file

hakuba = Hakuba(tableView: tableView)

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

hakuba[2]
	.append(cellmodel)	// append a new cell model into datasource
	.bump(.fade)		// show the cell of your cell model in the table view

hakuba[1]
	.remove(1...3)
	.bump(.Right)
// your cell swift file

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

	init(title: String, des: String, selectionHandler: @escaping (Cell) -> Void) {
		self.title = title
		self.des = des
		super.init(YourCell.self, selectionHandler: selectionHandler)
	}
}


class YourCell: Cell, CellType {
	typealias CellModel = YourCellModel

	@IBOutlet weak var titleLabel: UILabel!

	override func configure() {
		guard let cellmodel = cellmodel else {
			return
		}

		titleLabel.text = cellmodel.title
  	}
}

Usage

  • Initilization
private lazy var hakuba = Hakuba(tableView: tableView)   
  • Section handling
let section = Section() // create a new section

// inserting
hakuba
	.insert(section, at: 1)
	.bump()

// removing
hakuba
	.remove(at: index)
	.bump(.left)

hakuba
	.remove(section)
	.bump()

hakuba
	.removeAll()
	.bump()

// handing section index by enum
enum YourSection: Int, SectionIndexType {
	case top
	case center
	case bottom

	static let count = 3
}
	
let topSection = hakuba[YourSection.top]
  • Cell handling
// 1. appending
hakuba[0]
	.append(cellmodel)	// append a cellmodel
	.bump(.fade)		// and bump with `Fade` animation

hakuba[1]
	.append(cellmodels)	// append a list of cellmodes
	.bump(.left)					

// by using section
let section = hakuba[YourSection.top]
section
	.append(cellmodel)
	.bump()

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

// 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. removing
section
	.remove(at: 1)
	.bump(.right)

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

section
	.removeLast()
	.bump()
// updating cell data
let section = hakuba[YourSection.top]
section[1].property = newData
section[1]
	.bump()		
section.sort().bump()
section.shuffle().bump()
section.map
section.filter
section.reduce
section.mapFilter
section.each

section.first
section.last
section[1]
section.count
  • Register cell, header, footer
hakuba
	.registerCellByNib(CellClass.self)

hakuba
	.registerCell(CellClass.self)

hakuba
	.registerHeaderFooterByNib(HeaderOrFooterClass.self)

hakuba
	.registerHeaderFooter(HeaderOrFooterClass.self)

// register a list of cells by using variadic parameters
hakuba.registerCellByNibs(CellClass1.self, CellClass2.self, ..., CellClassN.self)
  • Section header/footer
let header = HeaderFooterViewModel(view: CustomHeaderView) {
	println("Did select header view")
}
hakuba[Section.top].header = header
  • Loadmore
hakuba.loadmoreEnabled = true
hakuba.loadmoreHandler = {
	// request api
	// append new data
}
  • Commit editing
hakuba.commitEditingHandler = { [weak self] style, indexPath in
self?.hakuba[indexPath.section]
	.remove(indexPath.row)
}
  • Deselect all cells
hakuba.deselectAllCells(animated: true)
  • Callback methods in the cell class
func willAppear(data: CellModel)
func didDisappear(data: CellModel)

Installation

  • Installation with CocoaPods
pod 'Hakuba'
  • Copying all the files into your project
  • Using submodule

Requirements

  • iOS 9.0+
  • Xcode 9+
  • Swift 4

License

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

Comments
  • Error when deleting row

    Error when deleting row

    Hello, I am trying to delete a row. But I got this crash :

    Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
    

    This is how I am trying to delete my row :

    self.models.value.removeFirst()
    self.hakuba[0].remove(0).slide(MYAnimation.Left)
    
    opened by remirobert 5
  • HeaderFooterView height, and backgroundColor

    HeaderFooterView height, and backgroundColor

    Hello, I need to use an HeaderFooterView on a Section.

    I created a model view :

    class HeaderSectionTripViewModel: HeaderFooterViewModel {
    
        init() {
            super.init(view: HeaderSectionTrip.self)
        }
    }
    

    and then my HeaderFooterView view :

    class HeaderSectionTrip: HeaderFooterView, HeaderFooterViewType {
        typealias ViewModel = HeaderFooterViewModel
    
        override func configure() {
        }
    }
    

    Now this how I add my section header :

    self.hakuba.registerHeaderFooterByNib(HeaderSectionTrip.self)
    
    let section = Section()
    section.header = HeaderSectionTripViewModel()
    self.hakuba.append(section)
    

    But my HeaderView is transparent. And also I need to change the height. thanks in advance for your help.

    opened by remirobert 3
  • Get a cell

    Get a cell

    Hi, is it posible to get a cell from :

    self.hakuba[index]...
    

    Because I have some UIButton, inside my cell, and I would like to catch the event, in my UIViewController.

    Thanks in advance. :+1:

    opened by remirobert 3
  • MYSelectionHandler parameter can't cast UITableViewCell.

    MYSelectionHandler parameter can't cast UITableViewCell.

    Hi, Thanks for writing this great Library.

    There is that one in trouble, It would help us if you would be compatible in the future.

    MYSelectionHandler parameter is MYBaseViewProtocol. I want to use the view of cell in the handler, but MYBaseViewProtocol can't cast UIView. Is not possible to change the IF of MYSelectionHandler to MYBaseViewProtocol of class that inherits from UIView?

    It is expected the correspondence. Thank you.

    opened by dekatotoro 2
  • Cell's calculatedHeight is always nil

    Cell's calculatedHeight is always nil

    Hey. I'm using Hakuba and so far it's great, but i think i found a bug. Cell's calculatedHeight property is always nil, even after all this calculations:

    private extension CellModel {
        func calculateHeight() -> CGFloat {
            if let height = calculatedHeight {
                return height
            }
    
            guard let cell = delegate?.getOffscreenCell(reuseIdentifier) else {
                return estimatedHeight
            }
    
            cell.configureCell(self)
    
            let width = delegate?.tableViewWidth() ?? UIScreen.mainScreen().bounds.width
            cell.bounds = CGRectMake(0, 0, width, cell.bounds.height)
            cell.setNeedsLayout()
            cell.layoutIfNeeded()
    
            let size = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
            return size.height + 1
        }
    }
    

    I think you should set it to a value before returning from function.

    opened by respan 1
  • Fix 'Invalid update' crash when removing all cells in tableview.

    Fix 'Invalid update' crash when removing all cells in tableview.

    When implementing a SearchBar and updating the results, a crash occurs with the message:

    "Invalid update: invalid number of sections. The number of sections contained in the table view after the update must be equal to the number of sections contained in the table view before the update, plus or minus the number of sections inserted or deleted."

    opened by beng341 1
  • Change the interface of MYTableViewCell.init

    Change the interface of MYTableViewCell.init

    Using generics arguments as before the change(public init<T: MYTableViewCell>(cell: T.Type, height: CGFloat = 44, selectionHandler: MYSelectionHandler? = nil)), it causes build errors when subclassing MYTableViewCell.

    opened by tasanobu-zz 1
  • dynamic height cell doesn't work

    dynamic height cell doesn't work

    The dynamic height cell is not working with a label in the cell. Is there any requirements ? I know there is the minimum lines set to 0

    Thanks in advance.

    opened by remirobert 0
  • how to get HeaderFooterView

    how to get HeaderFooterView

    I need to catch a UIButton action event present on a HeaderFooterView. So I have the HeaderFooterViewModel, but how to access to the view after presentation in my UIViewController ?

    Thanks in advance.

    opened by remirobert 0
  • dynamicHeightEnabled has a problem

    dynamicHeightEnabled has a problem

    if i delete delay(1.5) {} , dynamicHeightEnabled has a problem code:
    let longTitle1 = "Don't have to write the code for UITableViewDelegate and UITableViewDataSource protocols" let longTitle2 = "Support dynamic cell height from ios7"
    let titles = ["title 1", "title 2", "title 3", "title 4", "title 5", longTitle1, longTitle2]

        let centerCellmodels = titles.map { [weak self] title -> CellModel in
            let data = CustomCellModel(title: "Section 1: " + title) { _ in
                print("Did select cell with title = \(title)")
                self?.pushChildViewController()
            }
            data.dynamicHeightEnabled = true
            return data
        }
    
            centerSection
                .append(centerCellmodels)
                .bump(.Left)
    
    opened by bijy 0
Owner
Le Van Nghia
Software Engineer. Lover of OSS. Creator of PipeCD, Promviz, LotusLoad, Hakuba, MaterialKit... 🐳
Le Van Nghia
Server Driven UI can enable faster iterations and allowing apps to instantly update on multiple platforms.

Pets App Server Driven UI can enable faster iterations and allowing apps to instantly update on multiple platforms Steps to run Pets-Web: Download or

Metin Atalay 0 Jun 11, 2022
Cellmodel-driven collectionview manager

Sapporo [] (https://github.com/Carthage/Carthage) cellmodel-driven collectionview manager Features Easy to manage your sections and cells (reset/inser

Le Van Nghia 245 Sep 20, 2022
API-TableView-Swift - API call with URLSession and show data to TableView with swift

API-TableView-Swift API call with URLSession and show data to TableView with swi

Aman Ullah Akhand 1 Sep 3, 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
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
Poi - You can use tinder UI like tableview method

Poi You can use tinder UI like tableview method Installation Manual Installation Use this command git clone [email protected]:HideakiTouhara/Poi.git Imp

null 65 Nov 7, 2022
JNDropDownMenu - Easy to use TableView style dropdown menu.

Overview Swift version of https://github.com/dopcn/DOPDropDownMenu Easy to use TableView style dropdown menu. Setup The only thing you

Javal Nanda 65 Jun 27, 2021
ClassicPhotos is a TableView App demos how to optimize image download and filter with operation queue.

ClassicPhotos ClassicPhotos is a TableView App demos how to optimize image download and filter with operation queue. With Operation and Operation Queu

Kushal Shingote 2 Dec 18, 2021
iOS Application that gets the trending repositories data from Github API and displays it in a tableView.

Github-Trending-Repos iOS Application that gets the trending repositories data from Github API and displays it in a tableView. Follows MVC architectur

Anshul Koshyari 0 Oct 30, 2021
Recipe App TableView Using Swift

RecipeApp---TableView Features in Food Recipe App User Login search for recipes View recipe lists View Steps tutorials for recipes Like recipes In app

Hardik 3 Apr 27, 2022
Swift TableView pagination with async API request.

SwiftTableViewPagination Swift TableView pagination with async API request. Output UML Create puml file. $ cd SwiftTableViewPagination/scripts/swiftum

y-okudera 1 Dec 26, 2021
a TableView have thumbnail cell only, and you can use gesture let it expands other expansionView, all diy

ZYThumbnailTableView #####可展开型预览TableView,开放接口,完全自由定制 #####An expandable preview TableView, custom-made all the modules completely with open API you c

null 950 Oct 17, 2022
✨ Awesome expandable, collapsible tableview cell for iOS written in Swift 4

USE ExpandableCell. New version of this library. YNExpandableCell Updates See CHANGELOG for details Intoduction Easiest usage of expandable & collapsi

Kyle Yi 457 Dec 26, 2022
✨ Awesome expandable, collapsible tableview cell for iOS written in Swift 5

ExpandableCell Intoduction Fully refactored YNExapnadableCell with more concise, bug free. Easiest usage of expandable & collapsible cell for iOS, wri

Kyle Yi 728 Jan 1, 2023
Parallax scrolling effect on UITableView header view when a tableView is scrolled

ParallaxTableViewHeader Parallax scrolling effect on UITableView header view when a tableView is scrolled Usage Create a ParallaxHeaderView using eith

Vinodh Swamy 1.3k Nov 27, 2022
macOS Sqlite tableView 샘플 - objective c

목적 Objective C언어를 이용하여 macOS를 개발해본다. Sqlite를 이용하여 데이터를 저장하고, 불러와본다. FMDB를 이용한다. 데이터를 NSTableView를 이용하여 불러와본다. 추가적으로 NSOutlineView 구현해본다. 추가적으로 KVOCont

HyunSu Park 0 Jan 10, 2022
GrouponHeader - iOS TableView Header Animation, Swift/UIKit

GrouponHeader Description: iOS TableView Header Animation Technology: Swift, UIK

James Sedlacek 8 Dec 15, 2022
BountyList - A bounty list of One-Piece characters by using tableView

bountyList It shows a bounty list of One-Piece characters by using tableView str

null 0 Jan 18, 2022