ReusableViewExtensions
你是否憎恨通过字符串标识符注册UITableViewCell
?记住所有这些,一些在Storyboards中,一些隐藏在常数中某个地方?
NO MORE!使用ReusableViewExtensions!
安装
CocoaPods
ReusableViewExtensions可以通过CocoaPods使用。要安装它,只需将以下行添加到您的Podfile中
pod 'ReusableViewExtensions'
Carthage
ReusableViewExtensions可以通过Carthage使用。要安装它,只需将以下行添加到您的Cartfile中
github "agruchala/ReusableViewExtensions"
Swift包管理器
ReusableViewExtensions 可通过 Swift Package Manager 获取。要安装它,只需将以下 git 链接添加到您的包中
https://github.com/agruchala/ReusableViewExtensions.git
要求
- Swift 4.x
用法
如果您已安装框架,那么对于您的 UITableViewCell
和 UICollectionViewCell
,所有功能都应直接可用。要在数据源中获取单元格时使用它
- 将
import ReusableViewExtensions
添加到源文件 - 使用框架中的一个扩展方法,就这么简单!
代码片段
拥有一个单元格
class MyCell: UITableViewCell { }
无需向单元格添加任何符合,扩展适用于所有 UITableViewCell
和 UICollectionViewCell
。
在视表中注册它
使用 func register(_ cellType: Reusable.Type)
...
tableView.register(MyCell.self)
...
对于具有 xib
文件的单元格,使用 func register(nibType: Reusable.Type, bundle: Bundle = .main)
当需要时进行 deque 查询而不进行转换,只需使用类型!
使用 func dequeue<T: Reusable>(for indexPath: IndexPath) -> T
//File header
import ReusableViewExtensions
//Your data source
...
let cell: MyCell = tableView.dequeue(for: indexPath)
...
查看 示例,了解它如何与 xib
和从代码添加的单元格一起工作 :)
示例
要运行示例项目,请克隆仓库,然后在示例目录中首先运行 pod install
。
作者
我叫Artur,是一名来自波兰的开发者:)
许可证
ReusableViewExtensions 在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。