我有一个带模型和UITableViewDataSource
的动态UITableViewCell
我想在“ nth”行(例如第10行)中插入不同的UITableViewCell
...(对于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
的可重用功能的最佳方法是什么?>
我有一个包含模型的动态UITableViewDataSource和一个UITableViewCell,我想将一个不同的UITableViewCell插入“ nth”行(例如,第10行)(对于Google Ads)...