AutoDequeue
一种安全的方式来自动 dequeue motivationTableView, motivationCollectionView 和 motivationMapview 元素,无需调用 register 函数。元素注册,类型转换和错误处理都在单个 dequeue 方法内部。
要求
- iOS 9.0+
- Xcode 10.2+
- Swift 5.0+
安装
CocoaPods
CocoaPods 是一个 Cocoa项目的依赖管理器。有关使用和安装说明,请访问他们的网站。要使用 CocoaPods 将库集成到您的 Xcode 项目中,请在您的 Podfile
中指定它。
pod 'AutoDequeue', '1.2.0'
Swift 包管理器
Swift 包管理器 是一个用于自动分发 Swift 代码的工具,并集成到 swift
编译器中。
dependencies: [
.package(url: "https://github.com/berbschloe/AutoDequeue.git", from: "1.2.0")
]
使用
导入
建议全局添加AutoDequeue,因为它在所有地方导入都很烦人。
// Add this to a GlobalImports.swift
@_exported import AutoDequeue
如何为UITableView使用
旧方法
func viewDidLoad() {
super.viewDidLoad()
// register elements by a reuse id
tableView.register(CustomCell.self, forCellReuseIdentifier: "CELL_ID")
tableView.register(CustomHeader.self, forHeaderFooterViewReuseIdentifier: "HEADER_ID")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "CELL_ID", for: indexPath) as? CustomCell else {
fatalError("Invalid Class")
}
// setup cell
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let header = dequeueReusableHeaderFooterView(withIdentifier: "HEADER_ID") as? CustomHeader else {
fatalError("Invalid Class")
}
// setup header
return header
}
AutoDequeue方法
func viewDidLoad() {
super.viewDidLoad()
// no need to register elements
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CustomCell = tableView.dequeueReusableCell(for: indexPath)
// setup cell
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header: CustomHeader = dequeueReusableHeaderFooterView()
// setup header
return header
}
如何为UICollectionView使用
旧方法
func viewDidLoad() {
super.viewDidLoad()
// register elements by a reuse id
collectionView.register(CustomCell.self, forCellWithReuseIdentifier: "CELL_ID")
collectionView.register(CustomHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HEADER_ID")
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = dequeueReusableCell(withReuseIdentifier: "CELL_ID", for: indexPath) as? CustomCell else {
fatalError("Invalid Class")
}
// setup cell
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
guard let header = dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HEADER_ID", for: indexPath
) as? CustomHeader else {
fatalError("Invalid Class")
}
// setup header
return header
}
自动出队方式
func viewDidLoad() {
super.viewDidLoad()
// no need to register elements
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: CustomCell = dequeueReusableCell(for: indexPath)
// setup cell
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header: CustomHeader = dequeueReusableSupplementaryView(for: indexPath)
// setup header
return header
}
如何在 MKMapView 中使用
旧方法
func viewDidLoad() {
super.viewDidLoad()
// register elements by a reuse id
mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: "ANNOTATION_ID")
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "ANNOTATION_ID", for: annotation) as? CustomAnnotationView else {
fatalError("Invalid Class")
}
return annotationView
}
自动出队方式
func viewDidLoad() {
super.viewDidLoad()
// no need to register elements
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView: CustomAnnotationView = mapView.dequeueReusableAnnotationView(for annotation)
return annotationView