MYTableViewManager 1.0.1

MYTableViewManager 1.0.1

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后发布2015年3月
SPM支持 SPM

nghialv 维护。



Hakuba

我想瘦身我的视图控制器。

我想在不编写 UITableViewDelegateUITableViewDataSource 代码的情况下管理表格视图。

这就是我创建 Hakuba 的原因。

( Hakuba 是日本最著名的滑雪胜地之一。 )

功能

  • 不需要编写 UITableViewDelegateUITableViewDataSource 协议的代码
  • 轻松管理你的分区和单元格(追加/重置/插入/删除/更新)
  • 支持从 ios7 开始的动态单元格高度
  • 不需要担心单元格标识符
  • 使用尾随闭包处理单元格选择
  • 轻松实现头部/尾部视图(浮动回调)
  • 支持从 Nibs 或 Storyboards 创建单元格
  • 方法链
  • 索引器
  • 支持加载更多闭包
  • 完整示例
快速示例
    // 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 in datasource
             .slide(.Fade)          // show the cell of your cell model in the table view

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

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

        init(title: String, des: String, selectionHandler: MYSelectionHandler) {
            self.title = title
            self.des = des
            super.init(YourCell.self, selectionHandler: selectionHandler)
        }
    }


    class YourCell : MYTableViewCell {
        @IBOutlet weak var titleLabel: UILabel!

        override func configureCell(data: MYCellModel) {
            super.configureCell(data)
            if let cellmodel = data as? YourCellModel {
                titleLabel.text = cellmodel.title
            }
        }
    }

使用方法

  • 初始化
    private var hakuba = Hakuba(tableView: tableView)   
  • 分区处理
    let section = hakuba[secIndex]  // retrieve a section or create a new section if it doesn't already exist

    // inserting
    hakuba.insert(section, atIndex: 1)
          .slide()

    // removing
    hakuba.remove(index)
          .slide(.Left)

    hakuba.removeAll()
          .slide()

    // handing section index by enum
    enum Section : Int, MYSectionIndex {
        case Top = 0
        case Center
        case Bottom

        var intValue: Int {
            return self.rawValue
        }
    }
    let topSection = hakuba[Section.Top]
  • 单元格处理
    // 1. appending
    hakuba[0].append(cellmodel)             // append a cellmodel
             .slide(.Fade)                  // and slide with `Fade` animation

    hakuba[1].append(cellmodels)            // append a list of cellmodes
            .slide(.Left)                   

    // by using section
    let section = hakuba[Section.Top]
    section.append(cellmodel)
           .slide()


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


    // 3. reseting
    section.reset(cellmodels)               // replace current data in section by the new data
           .slide()
    section.reset()                         // or remove all data in section
           .slide()


    // 4. removing
    section.remove(1)
           .slide(.Right)
    section.remove(2...5)
           .slide()
    section.removeLast()
           .slide()
    // updating cell data
    let section = hakuba[Section.Top]
    section[1].property = newData
    section[1].slide()      
    section.sort().slide()
    section.shuffle().slide()
    section.map
    section.filter
    section.reduce
    section.mapFilter
    section.each

    section.first
    section.last
    section[1]
    section.count
  • 创建单元格模型
    // create a cell model
    let cellmodel = MYCellModel(cellClass: YourCell.self, userData: celldata) {
        println("Did select")
    }

    // create a list of cell models from api results
    let items = [...] // or your data from API

    let cellmodels = items.map { item -> MYCellModel in
        return MYCellModel(cellClass: YourCell.self, userData: item) {
            println("Did select cell")
        }
    }
  • 注册单元格、头部、尾部
    hakuba.registerCellNib(CellClassName)
    hakuba.registerCellClass(CellClassName)
    hakuba.registerHeaderFooterNib(HeaderOrFooterClassName)
    hakuba.registerHeaderFooterClass(HeaderOrFooterClassName)

    // register a list of cells by using variadic parameters
    hakuba.registerCellNib(CellClass1.self, CellClass2.self, ..., CellClassN.self)
  • 分区头部/尾部
    let header = MYHeaderFooterViewModel(viewClass: CustomHeaderView.self, userData: yourData) {
        println("Did select header view")
    }
    hakuba[Section.Top].header = header

    // hide header in section 1
    hakuba[Section.Center].header?.enabled = false
  • 加载更多
    hakuba.loadmoreEnabled = true
    hakuba.loadmoreHandler = {
        // request api
        // append new data
    }
  • 提交编辑
    hakuba.commitEditingHandler = { [weak self] style, indexPath in
        self?.hakuba[indexPath.section].remove(indexPath.row)
    }
  • 取消选择所有单元格
    hakuba.deselectAllCells(animated: true)
  • 动态单元格高度:当你想要启用动态单元格高度时,你只需要设置估计高度参数的值,并将 dynamicHeightEnabled = true 设置为 true
    let cellmodel = MYCellModel(cellClass: YourCell.self, height: 50, userData: yourCellData) {
        println("Did select cell")
    }
    cellmodel.dynamicHeightEnabled = true
  • 单元格类中的回调方法
    func willAppear(data: MYCellModel)
    func didDisappear(data: MYCellModel)

安装

  • 使用 CocoaPods 进行安装
    pod 'Hakuba'
  • 将所有文件复制到你的项目中
  • 使用子模块

需求

  • iOS 7.0+
  • Xcode 6.1

许可证

Hakuba 是在 MIT 许可下发布的。查看 LICENSE 以获取详细信息。