为什么使用EasyList?
EasyList是UITableView的子类,可以帮助您以更少的代码、更易于阅读和维护的方式创建类型安全的列表。
正如您所知的,使用UITableView需要实现代理模式,如果是不同类型的单元格,则需要switch语句(强制转换UITableViewCell到您的自定义单元格类型),以及如'cellIdentifier'这样的冗余代码。
EasyList是处理简单到复杂表格视图的新方法;它为您的逻辑提供了封装,因此您有了更少的样板代码。
基本用法
配置类型:EasyListConfigurationDefault,支持多种单元格类型,静态高度
let cellConfiguration = CellConfiguration { (cell, indexPath) -> YourCustomCell in
cell.setText(self.dataSource[indexPath.row])
return cell
}
let config = EasyListConfigurationDefault.init(
cellConfiguration: { indexPath -> CellConfigurationType in
return cellConfiguration
}, dataSourceCount: { () -> Int in
return self.dataSource.count
}, rowHeight: { indexPath -> CGFloat in
return 50
}) { (selectedCell, selectedIndexPath) in
///Did select cell
}
self.easyList = EasyList.init(config)
高级用法
配置类型:EasyListConfigurationAutoSizingCells,支持多种单元格类型,动态单元格高度
let cellConfiguration = CellConfiguration { (cell, indexPath) -> YourCustomCell in
cell.setText(self.dataSource[indexPath.row])
return cell
}
let cellConfiguration2 = CellConfiguration { (cell, indexPath) -> YourCustomCellWithDinamicSize in
cell.setText(self.dataSource[indexPath.row])
return cell
}
let config = EasyListConfigurationAutoSizingCells.init(
cellConfiguration: { indexPath -> CellConfigurationType in
if (indexPath.row == 5) {
return cellConfiguration2
}
return cellConfiguration
}, dataSourceCount: { () -> Int in
return self.dataSource.count
}, estimatedRowsHeight: 100) { (selectedCell, selectedIndexPath) in
///Did select cell
}
self.easyList = EasyList.init(config)
详细说明
使用'CellConfiguration'块来定义不同的单元格类型。"它的返回类型应为您的自定义单元格,并设置它的参数。在创建好所需的单元格配置块后,选择您的'EasyListConfiguration'类型。
- EasyListConfigurationAutoSizingCells:用于自动调整单元格大小
- EasyListConfigurationDefault:静态单元格大小
创建它,传递CellConfiguration作为索引路径,以及其余参数(应该会自动完成)
调用reload与任何UITableView一样(它实际上是表格视图),使用:"reloadData()"
然后您就可以开始使用了!(
安装
Cocoapods
EasyList 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'EasyList'
手动
- 下载并将
/EasyList
文件夹拖放到您的项目中。 - 恭喜!
作者
Matan 使用