GenericCells
创建 UITableViewCell 和 UICollectionViewCell 子类在 Swift 2.0 中是如此简单。
介绍 GenericCells!…利用 Swift 的泛型功能,我们可以取得自定义 UITableViewCell 或 UICollectionViewCell 类的所有好处,而不必为每个这样的类创建一个新的类。
让我们看看如何使用它。
为所需的布局创建一个 UIView 子类
final class MyView: UIView {
let imageView: UIImageView
let titleLabel: UILabel
let subtitleLabel: UILabel
// And add more views to your heart's content
}
用一个 UITableView 或 UICollectionView 将该单元注册
tableView.register(GenericTableCell<MyView>.self)
collectionView.register(GenericCollectionCell<MyView>.self)
使用 UITableView 或 UICollectionView 来抓取单元
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as GenericTableCell<MyView>
let cell = collectionView.dequeueReusableCell(forIndexPath: indexPath) as GenericCollectionCell<MyView>
然后按需调整它。您想要访问的视图将通过 .customView
属性提供
cell.customView.titleLabel.text = "This library rules!"
cell.customView.imageView.image = UIImage(named: "success")
加分项,让我们添加回收支持。扩展您的视图以包含 ReusableGenericView
协议
extension MyView: ReusableGenericView {
func prepareForReuse() {
self.titleLabel.text = ""
self.subtitleLabel.text = ""
self.imageView.image = nil
}
}
当单元格被回收时,将调用它。
安装
您可以使用 SPM 安装 GenericCells
。
您还可以通过将 GenericCells
添加到 Podfile
文件,使用 CocoaPods 来安装。
platform :ios, '9.0'
use_frameworks!
pod 'GenericCells'
或者,您可以手动安装,方法是从服务器下载 GenericTableCell.swift
、GenericCollectionCell.swift
和 ReusableView.swift
文件,并将其放入您的项目中。
关于我
嗨,我在网络上到处都可以找到我,但更特别的是我在 Twitter。
许可证
有关如何使用 GenericCells 的更多信息,请参阅 许可证。
就这样了吗?
是的,就这样。如果你不喜欢,很抱歉,这里有一道彩虹。