gpt4 book ai didi

swift - 如何在自定义表格 View 单元格中创建自定义 View ?

转载 作者:可可西里 更新时间:2023-11-01 01:08:10 26 4
gpt4 key购买 nike

我正在尝试在自定义表格 View 单元格内创建自定义复选框。

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

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 位置不对吗?

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

最佳答案

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

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

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

在您的情况下,ViewController 负责Cell 的 subview 布局和调整。在 SOLID 中至少打破 ~S~

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

关于swift - 如何在自定义表格 View 单元格中创建自定义 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52971180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com