gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:38:04 25 4
gpt4 key购买 nike

我有以下用于各种 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com