将单元格添加到动态UITableView数据源|斯威夫特

问题描述 投票:1回答:1

我有一个带模型和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)...

swift uitableview model datasource
1个回答
0
投票
我想到了:
© www.soinside.com 2019 - 2024. All rights reserved.