LoginSignup
7
6

More than 5 years have passed since last update.

[Swift]Hakubaの使い方

Last updated at Posted at 2016-09-03

Hakubaとは

Hakubaとは,UITableViewをCellmodel-derivenで扱うためのマネージャー.
Hakubaを用いることで,UITableViewDelegateUITableViewDataSourceを実装することなくUITableViewを管理することができ,すっきりしたview controllerを実現できる.

はじめに

Hakubaの使い方を学ぶために,UITableViewのセルを追加・削除できる簡単なアプリを作りました.
今回のソースコードは,GitHubにアップしたので,参考にどうぞ.

環境

  • OSX El Capitan
  • Swift 2.2
  • iOS 8.0
  • Xcode 7.3
  • Hakuba 2.1.1

CellとCellModelの実装

Hakubaを使うために,まずCellとCellModelを実装します.

MyCell.swift
import UIKit
import Hakuba

class MyCell: Cell, CellType {

    typealias CellModel = MyCellModel

    @IBOutlet weak var label: UILabel!

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

        cellmodel.editable = true
        cellmodel.editingStyle = .Delete  
    }
}

MyCellModel.swift
import UIKit
import Hakuba

class MyCellModel: CellModel {
    let text = String()

    init() {
        super.init(cell: MyCell.self, selectionHandler: nil)
    }
}

ViewControllerの実装

次にコントローラを実装します.
Hakubaで管理したいUITableViewをHakubaのコンストラクタに渡します.

viewController.swift
import UIKit
import Hakuba

class ViewController: UIViewController {
    private let  tableView = UITableView()
    private lazy var hakuba: Hakuba = Hakuba(tableView: self.tableView)

    override func viewDidLoad() {
        super.viewDidLoad()

        configureNavigationBar()
        configureAppearance()
        configureHakuba()
    }

    func tapAddBtn(){
        hakuba[0]
            .append(MyCellModel())
            .bump(.Middle)
    }

    override func setEditing(editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        hakuba.setEditing(editing, animated: animated)
    }

    func deleteCell(indexPath: NSIndexPath){
        hakuba[indexPath.section]
            .remove(indexPath.row)
            .bump()
    }

}

// MARK - private methods
private extension ViewController {

    func configureAppearance(){
        tableView.allowsSelection = false
        tableView.frame = view.frame
        view = tableView
    }

    func configureNavigationBar(){
        let addBtn = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: #selector(tapAddBtn))
        navigationItem.setRightBarButtonItem(addBtn, animated: true)
        navigationItem.leftBarButtonItem = editButtonItem()
    }

    func configureHakuba(){
        hakuba
            .registerCellByNib(MyCell)
            .commitEditingHandler = { [weak self] style, indexPath in
                if (style == UITableViewCellEditingStyle.Delete) {
                    self?.deleteCell(indexPath)
                }
            }

        // 以下のように,repeatedValue にクラスを渡すと実体は1つのため状態が共有されてしまうので,実際に使うときは注意
        let cells: [MyCellModel] = Array(count: 4, repeatedValue: MyCellModel())
        let section = Section()

        hakuba
            .reset(section)
            .bump()

        section
            .reset(cells)
            .bump()
    }
}

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6