MDTable 0.3.0

MDTable 0.3.0

测试测试
Lang语言 SwiftSwift
许可证 MIT
发布最新版本2017 年 7 月
SwiftSwift 版本3.0
SPM支持 SPM

LeoMobileDeveloper 维护。



MDTable 0.3.0

  • 作者:
  • Leo

MDTables

MDTable 是一个 数据驱动的 UITableView

let row0 = Row(title: "System Cell", accessoryType: .disclosureIndicator)
row0.onDidSelected { (tableView, indexPath) in
    tableView.deselectRow(at: indexPath, animated: true)
}
let row1 = Row(title: "Custom Cell", accessoryType: .disclosureIndicator)
    
let section0 = Section(rows: [row0,row1])
section0.titleForHeader = "Basic"
section0.heightForHeader = 30.0
    
tableView.manager = TableManager(sections: [section0])

您的 tableView 已经准备好了。

示例

示例项目包含一个 网易云音乐 的示例。(以网易云音乐首页作为示例)

要求

  • Xcode 8.1+
  • iOS 8.0+
  • Swift 3.0+

安装

CocoaPod

pod "MDTable"

用法

基本概念

MDTable 提供两种基本类型

  • Row - Cell 的模型。
  • Section- Section 的模型

创建行和 Section。

let row = Row(title: "System Cell", accessoryType: .disclosureIndicator)
let section0 = Section(rows: row)

然后使用声明式 API 处理事件

row.onWillDisplay { (tableView, cell, indexPath) in
    //Access manager with tableView.manager
}
row.onDidSelected { (tableView, indexPath) in
    
}

然后创建一个管理器并将其绑定到 tableView

tableManager = TableManager(sections: [section0])
tableView.md_bindTo(manager: tableManager)

自定义 Cell

模型

创建 ReactiveRow 的子类

class XibRow:ReactiveRow{
    //Data
    var title:String
    var subTitle:String
    var image:UIImage
    init(title:String, subTitle:String, image:UIImage) {
        self.title = title
        self.subTitle = subTitle
        self.image = image
        super.init()
        self.rowHeight = 80.0
        self.reuseIdentifier = "XibRow"
        self.initalType = RowConvertableInitalType.xib(xibName: "CusomCellWithXib")
    }

}

Cell

创建 MDTableViewCell 的子类,并重写 render

class CusomCellWithXib: MDTableViewCell{    
    override func render(with row: TableRow) {
        guard let row = row as? XibRow else{
            return;
        }
        //Render the cell 
    }
}

魔法发生的地方

let row = XibRow(title: "Title", subTitle: "Subtitle", image: UIImage(named: "avatar")!)
row.onDidSelected({ (tableView, indexPath) in
    tableView.manager?.delete(row: indexPath)
    tableView.deleteRows(at: [indexPath], with: .automatic)
})
let section = Section(rows: rows)
section.heightForHeader = 30.0
section.titleForHeader = "Tap Row to Delete"
tableView.manager = TableManager(sections: [section])

注意

您需要使用 [unowned self] 来避免保留循环

row.onDidSelected = { [unowned self] (tableView, indexPath) in
}

作者

Leo, [email protected]

许可证

MDTable 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。