diff --git a/Example/Example/CellTestViewController.swift b/Example/Example/CellTestViewController.swift index d74090f..49fce8e 100644 --- a/Example/Example/CellTestViewController.swift +++ b/Example/Example/CellTestViewController.swift @@ -8,6 +8,13 @@ import UIKit +enum SectionIndex: Int, SectionIndexType { + case Top + case Center + + static let count = 2 +} + class CellTestViewController: UIViewController { @IBOutlet weak var tableView: UITableView! private lazy var hakuba: Hakuba = Hakuba(tableView: self.tableView) @@ -30,14 +37,14 @@ class CellTestViewController: UIViewController { self?.pushChildViewController() } } - - let topSection = Section() - let centerSection = Section() - + hakuba - .reset([topSection, centerSection]) + .reset(SectionIndex) .bump() + let topSection = hakuba[SectionIndex.Top] + let centerSection = hakuba[SectionIndex.Center] + topSection .reset(topCellmodels) .bump() diff --git a/Hakuba/Common.swift b/Hakuba/Common.swift index 4c984c5..fd7ff3a 100644 --- a/Hakuba/Common.swift +++ b/Hakuba/Common.swift @@ -12,8 +12,15 @@ public typealias SelectionHandler = (Cell) -> () public typealias Animation = UITableViewRowAnimation -public protocol SectionIndex { +public protocol SectionIndexType { var intValue: Int { get } + static var count: Int { get } +} + +public extension SectionIndexType where Self: RawRepresentable, Self.RawValue == Int { + var intValue: Int { + return rawValue + } } public enum HakubaBumpType { diff --git a/Hakuba/Hakuba.swift b/Hakuba/Hakuba.swift index c9a046e..3d691c0 100644 --- a/Hakuba/Hakuba.swift +++ b/Hakuba/Hakuba.swift @@ -42,7 +42,7 @@ final public class Hakuba: NSObject { public var cellEditable = false public var commitEditingHandler: ((UITableViewCellEditingStyle, NSIndexPath) -> ())? - public subscript(index: SectionIndex) -> Section { + public subscript(index: SectionIndexType) -> Section { get { return self[index.intValue] } @@ -73,7 +73,7 @@ final public class Hakuba: NSObject { return sections.get(index) } - public func getSection(index: SectionIndex) -> Section? { + public func getSection(index: SectionIndexType) -> Section? { return getSection(index.intValue) } @@ -148,6 +148,11 @@ public extension Hakuba { public extension Hakuba { // MARK - Reset + func reset(listType: SectionIndexType.Type) -> Self { + let sections = (0...listType.count).map { _ in Section() } + return reset(sections) + } + func reset() -> Self { return reset([]) }