【发布时间】:2020-06-03 06:09:23
【问题描述】:
我有一个动态的UITableViewDataSource,它接受一个模型和一个UITableViewCell
我想将不同的UITableViewCell 插入到“第 n”行(例如第 10 行)...(对于 Google Ads)
数据源
class ModelTableViewDataSource<Model, Cell: UITableViewCell>: NSObject, UITableViewDataSource {
typealias CellConfigurator = (Model, Cell) -> 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) as! Cell
cellConfigurator(model, cell)
return cell
}
}
然后我为每个模型添加一个扩展
型号
extension ModelTableViewDataSource where Model == SomeModel, Cell == SomeCell {
static func make(for someModel: [SomeModel], reuseIdentifier: String = "Identifier") -> ModelTableViewDataSource {
return ModelTableViewDataSource(models: someModel, reuseIdentifier: reuseIdentifier) { (someModel, someCell) in
someCell.model = someModel
}
}
}
保持UITableViewDataSource的可重用功能的最佳方法是什么
【问题讨论】:
-
您希望该广告在
make函数创建的每个 数据源中,还是仅在一个特殊的数据源中? -
仅在选定的几个中。
-
然后我会创建一个不同的
make函数,它会创建具有预期行为的ModelTableViewDataSource的子类。 -
将尝试按照这些思路构建一些东西。如果您有任何示例将不胜感激。
标签: swift uitableview model datasource