【问题标题】:Configure Custom TableViewCell Outside of TableView?在 TableView 之外配置自定义 TableViewCell?
【发布时间】:2017-08-30 23:26:38
【问题描述】:

我有一个带有自定义单元格的 TableView,它需要相当长的配置并且在我的应用程序中多次使用。我想避免重复的代码,只在一个地方配置单元格。我可以创建这样的函数吗?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "betterPostCell", for: indexPath) as! BetterPostCell

        return configureCell(cell)
}

理想情况下,我可以将configureCell 放入我的 BetterPostCell 类中。这可能吗?

【问题讨论】:

  • 在 BetterPostCell 类中添加 configure<T>(withItem item:T) 您将调用 cell.configure(withItem: _item) 并返回单元格

标签: ios swift uitableview


【解决方案1】:

是的,您可以这样做,这是防止表格视图代码崩溃的好方法,尤其是当您在一个表格视图中有许多不同类型的单元格时。

在您的 BetterPostCell 类中,创建一个名为 configure 的方法,如下所示:

func configure() {
     //configure your cell
}

然后在您的 cellForRowAt 方法中,只需从您的单元格中调用该方法:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "betterPostCell", for: indexPath) as! BetterPostCell
        cell.configure()
        return cell
}

【讨论】:

    【解决方案2】:

    您可以使用configure 函数和关联类型Cell 创建协议。使用协议扩展,您可以为不同的单元格类型添加默认实现以及其他方法。

    protocol CellConfigurable {
        associatedType Cell
    
        func configure(_ cell: Cell)
    }
    
    extension CellConfigurable where Cell == SomeTableViewCell {
    
        func configure(_ cell: SomeTableViewCell) {
            ...
        }
    }
    

    【讨论】:

    • 在哪里写这个扩展
    【解决方案3】:
    try this code to create CustomCell:- 
    
    class CustomCell: UITableViewCell {
    
        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            self.initViews()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            self.perform(#selector(self.initViews), with: self, afterDelay: 0)
        }
    
        //MARK: Init views
        func initViews()   {
            //Add your code 
        }
    
        //MARK: Layout subviews
    
        override func layoutSubviews() {
            super.layoutSubviews()
        // Here you can code for layout subviews
        }
    
        //MARK: Update all valuesw model
    
        func updateWithModel(_ model: AnyObject) { 
        //here you can update values for cell  
        }
    
    }
    
    
    //Call CustomCell in Tableview class
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomCell
            cell.updateWithModel(items[indexPath.row])
            return cell
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      • 2014-05-05
      • 1970-01-01
      相关资源
      最近更新 更多