【问题标题】:Adding Cells to Dynamic UITableView DataSource | Swift将单元格添加到动态 UITableView 数据源 |迅速
【发布时间】: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


【解决方案1】:

我想到了这个:

  • 创建一个子类
  • 覆盖 numberOfRowsInSection 以返回行数 + 1
  • 覆盖 cellForRowAt 以处理第 10 行

    类 ModelTableViewDataSourceWithAd: ModelTableViewDataSource {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return super.tableView(tableView, numberOfRowsInSection:section) + 1
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (indexPath.row == 9) {
            // return Google ad cell
        } else {
            return super.tableView(tableView, cellForRowAt:indexPath)
        }
    }
    

    }

然后是创建者:

extension ModelTableViewDataSource where Model == SomeModel, Cell == SomeCell {

// ...

    static func makeWithAd(for someModel: [SomeModel], reuseIdentifier: String = "Identifier") -> ModelTableViewDataSource {
        return ModelTableViewDataSourceWithAd(models: someModel, reuseIdentifier: reuseIdentifier) { (someModel, someCell) in
            someCell.model = someModel
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    相关资源
    最近更新 更多