MzTableView 0.0.1

MzTableView 0.0.1

Mohammad Zakizadeh 维护。



MzTableView

CI Status Version License Platform

简介

只需2行代码即可实现 TableView!对于为您的 UITableView 提供数据,不再需要 prezenCellForRowAtIndexPath、numberOfRowsInSection 和其他样板代码!请注意,目前尚不支持多节、头部和尾部

要求

您的 tableView 必须满足以下要求,否则 MzTableView 将无法为您工作::

  • 您的 tableView 只包含一种类型的 cell
  • 您的 tableView 只包含一个分区

90% 的 tableView 设计都满足这些要求!

示例

要运行示例项目,请先克隆仓库,然后从示例目录中运行 pod install

安装

MzTableView 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile

pod 'MzTableView'

演示

您可以在 example 文件夹中轻松找到并运行演示应用程序。

用法

首先,您的单元格应遵循 MzTableViewCell 协议

public protocol MzTableViewCell: UITableViewCell {
    associatedtype CellViewModel
    func configureCellWith(_ item: CellViewModel)   
}

CellViewModel 是单元格的 ViewModel,用于为单元格的子视图分配数据,然后使用 configureCellWith 方法配置单元格。例如,我们想显示学生的列表,我们的单元格有一个图像和一个标题,因此我们的 CellViewModel 看起来像

struct StudentCellViewModel {
    var image: UIImage
    var title: String
}

因此您的单元格看起来像

extension ExampleTableViewCell: MzTableViewCell {
    
    typealias CellViewModel = StudentCellViewModel
    
    func configureCellWith(_ item: StudentCellViewModel) {
        cellTitle.text = item.title
        cellImage.image = item.image
    }
}

然后在您的 UIViewController 中,您应该创建 MzTableViewDataSource 对象

public init(cellHeight: CGFloat?, tableView: UITableView, items: [T.CellViewModel] = [], animationType: AnimationType = .none
  • cellHeight 是 tableView 单元格的高度(对于自动调整大小的单元格返回 0 或 nil)
  • tableView 是 UIViewController 的 UITableView。
  • items 是 tableView 的初始项(您可以将空数组放置其中,然后稍后向其中添加,如下所示。)
  • animationType 是一个可选枚举,如果要对单元格进行动画处理(.none 是默认值)

然后将对象分配给您的 tableView 数据源和代理。

     self.exampleTableView.dataSource = mzDataSource
     self.exampleTableView.delegate = mzDataSource

要向您的 tableView 添加数据,只需使用

mZdataSource.appendItemsToTableView([items])

其中 items 是单元格的 ViewModel 数组。( CellViewModel )

不再需要重新加载数据、插入行等。MzDataSource 处理所有!”

您也可以使用以下内容从您的 tableView 中删除项目

mZdataSource.removeItemFromTableView(row: rowToDelete)

或者您可以刷新您的 tableView 以新的数据

mZdataSource.refreshWithNewItems(items)

如果使用说明不起作用,请运行并查看示例项目。这将提高您的能力😇.

作者

mohammadz74, [email protected]

许可

MzTableView遵循MIT许可证。有关更多信息,请参阅LICENSE文件。