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

标签 ios swift model datasource

我有以下用于各种 tableView 的外部数据源。

我想让它动态化,并能够将 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
        }
    }
}

关于ios - 如何使用外部数据源转换单元格类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57961889/

相关文章:

ruby-on-rails - Rails 上的 ruby ActiveRecord : pluralization

iOS 7 自动亮度正在否决应用程序屏幕亮度设置

ios - 如何在 tvOS 上深度链接到 Netflix?

ios - Restkit 结冰了

ios - 如何从 AppDelegate 显示表单?

database - CakePHP 数据库表,缺少数据源默认值

ios - 看不到 "Hold for Developer Release"选项

ios - 为什么 Swift 2.2 不支持固定大小的数组,也许 swift 3.0 支持?

ios - 离线地址获取

zend-framework - Zend 框架模块无法找到/加载模型