Ios 将不同的单元格对象插入到XPath中
我有一个带有自定义单元格(单元格a)列表的Ios 将不同的单元格对象插入到XPath中,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个带有自定义单元格(单元格a)列表的UITableView。我想在模型的indepath第n行插入一个不同的单元格类型(单元格B)。但是,我使用动态扩展来创建模型 这是我的数据源代码 class HomeViewDataSource<Model, Cell: UITableViewCell>: NSObject, UITableViewDataSource { typealias CellConfigurator = (Model, Cell) -> Void var
UITableView
。我想在模型的indepath
第n行插入一个不同的单元格类型(单元格B)。但是,我使用动态扩展来创建模型
这是我的数据源
代码
class HomeViewDataSource<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 HomeViewDataSource where Model == Ad, Cell == AdTableViewCell {
static func make(for ad: [Ad], viewController: UIViewController,
reuseIdentifier: String = Identifier.adTableViewCell) -> HomeViewDataSource {
return HomeViewDataSource(models: ad, reuseIdentifier: reuseIdentifier) { (ad, adTableViewCell) in
adTableViewCell.ad = ad
}
}
}
使用此数据源结构插入另一个单元格类型的最佳方法是什么 扩展
HomeViewDataSource
以处理多个单元格类型或将其删除。如何处理多个单元格?这取决于HomeViewDataSource
func configureTableViewDataSource(_ recentAds: [Ad]) {
dataSource = SectionedHomeViewDataSource(dataSources: [
HomeViewDataSource.make(for: recentAds, viewController: self)])
tableView.dataSource = dataSource
DispatchQueue.main.async {
self.tableView.reloadData()
}
}