【问题标题】:How do I create a custom view in a custom table view cell?如何在自定义表格视图单元格中创建自定义视图?
【发布时间】:2026-02-06 17:35:01
【问题描述】:

我正在尝试在自定义表格视图单元格中创建自定义复选框。

我有一个自定义表格视图单元格:

class CustomTableViewCell: UITableViewCell {
   weak var delegate: CellDelegateT?
   @IBOutlet var customCheckbox: VKCheckbox!
}

这是加载我的 tableView 的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TransactionsTableViewCell
    let customCheckbox = cell.customCheckbox!
    customCheckbox.line = .thin
    customCheckbox.bgColor = UIColor.white
    customCheckbox.color = UIColor.black
    customCheckbox.borderColor = UIColor.black
    customCheckbox.borderWidth = 2
    customCheckbox.cornerRadius = customCheckbox.frame.height / 2
}

我需要将构造复选框的代码放在哪里? cellForRowAt 位置不对吗?

当我在表格单元格之外使用代码时,它可以正常工作。这就是为什么我认为我将复选框创建代码放在错误的位置。

【问题讨论】:

    标签: swift xcode uitableview


    【解决方案1】:

    你的问题的答案是“是的,你应该把复选框设置到另一个地方。特别是我建议你把它移到CustomTableViewCell类”

    有一种观点认为,良好的面向对象软件设计基于SOLID 原则。

    SOLID 是首字母缩略词。 S 代表Single responsibility principle.。简单地说——一个类应该负责一件事,这个职责应该被类封装。

    在您的情况下,ViewController 负责 Cells 子视图的布局和调整。在 SOLID 中至少破坏了 ~S~

    P.S.说实话,我有点害怕写这个答案,因为经常争论关于好的软件设计。

    【讨论】:

      【解决方案2】:

      一般来说,建议您不要将构建复选框的代码放在cellForRowAt 中,正如@fewlinesofcode 已完美解释的那样。以下是我个人推荐的另外两个回答您的问题的替代方案。希望这会有所帮助...

      选项 1:

      class CustomTableViewCell: UITableViewCell {
          @IBOutlet var customCheckbox: VKCheckbox!
      
          func checkBoxControl() {
              customCheckbox.line = .thin
              customCheckbox.bgColor = UIColor.white
              customCheckbox.color = UIColor.black
              customCheckbox.borderColor = UIColor.black
              customCheckbox.borderWidth = 2
              customCheckbox.cornerRadius = customCheckbox.frame.height / 2
          }
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TransactionsTableViewCell else { return UITableViewCell() }
          cell.checkBoxControl()
          return cell
      }
      

      选项 2:

      class CustomTableViewCell: UITableViewCell {
          @IBOutlet var customCheckbox: VKCheckbox!
      
          override func awakeFromNib() {
              super.awakeFromNib()
      
              customCheckbox.line = .thin
              customCheckbox.bgColor = UIColor.white
              customCheckbox.color = UIColor.black
              customCheckbox.borderColor = UIColor.black
              customCheckbox.borderWidth = 2
              customCheckbox.cornerRadius = customCheckbox.frame.height / 2
          }
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TransactionsTableViewCell else { return UITableViewCell() }
          return cell
      }
      

      【讨论】:

        最近更新 更多