如何使用外部数据源强制转换单元格类型



我有以下外部数据源,用于各种表视图。

我想让它动态化,并能够将CellConfigurator中的UITableViewCell转换为各种自定义单元格。 我在下面为每个模型做一个扩展。 但是我需要能够在扩展中转换为不同的单元格类型。

import UIKit
class ProductSearchDataSource<Model>: NSObject, UITableViewDataSource {
typealias CellConfigurator = (Model, UITableViewCell) -> Void
var models: [Model]
private let reuseIdentifier: String
private let cellConfigurator: CellConfigurator
init(models: [Model], reuseIdentifier: String, cellConfigurator: @escaping CellConfigurator) {
self.models = models
self.reuseIdentifier = reuseIdentifier
self.cellConfigurator = cellConfigurator
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return models.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = models[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
cellConfigurator(model, cell)
return cell
}
}
extension ProductSearchDataSource where Model == ProductSearchHistory {
static func make(for productSearch: [ProductSearchHistory], reuseIdentifier: String = "productSearchTableViewCell") -> ProductSearchDataSource {
return ProductSearchDataSource(models: productSearch, reuseIdentifier: reuseIdentifier) { (productSearch, productSearchTableViewCell) in
productSearchTableViewCell.textLabel?.text = productSearch.searchHistory
}
}
}

您需要为单元格引入额外的占位符类型:

class ProductSearchDataSource<Model, Cell: UITableViewCell>: NSObject, UITableViewDataSource {
typealias CellConfigurator = (Model, Cell) -> Void
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = models[indexPath.row]
// Note: You may want to handle the downcast better...
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! Cell
cellConfigurator(model, cell)
return cell
}
}
extension ProductSearchDataSource where Model == ProductSearchHistory, Cell == ProductSearchCell {
static func make(for productSearch: [ProductSearchHistory], reuseIdentifier: String = "productSearchTableViewCell") -> ProductSearchDataSource {
return ProductSearchDataSource(models: productSearch, reuseIdentifier: reuseIdentifier) { (productSearch, productSearchTableViewCell) in
// `productSearchTableViewCell` will now have a type of `ProductSearchCell `.
productSearchTableViewCell.textLabel?.text = productSearch.searchHistory
}
}
}

最新更新