设置单元格的更好方法
Better way to set cell
我开始使用 MVC 编写我的第一个应用程序,但我不知道代码版本在这种模式下更好。
在viewcontroller中:
let quantity = quantityProducts[indexPath.row]
let nameProduct = productsArray[indexPath.row]
cell.configurateWithItem(quantity: quantity, name: nameProduct)
在细胞类中:
class NeedProductsTableViewCell: UITableViewCell {
@IBOutlet weak var quantityLabel: UILabel!
@IBOutlet weak var productNameLabel: UILabel!
func configurateWithItem(quantity:String,name:String){
quantityLabel.text = quantity
quantityLabel.textColor = .systemGray
quantityLabel.font = quantityLabel.font.withSize(14)
productNameLabel.text = name
productNameLabel.font = UIFont.boldSystemFont(ofSize: 14)
}
或者可能是标准版本(在视图控制器中设置单元格属性)?
let cell = tableView.dequeueReusableCell(withIdentifier: "NeedProductsTableViewCell", for: indexPath) as! NeedProductsTableViewCell
cell.quantityLabel.text = quantityProducts[indexPath.row]
cell.quantityLabel.textColor = .systemGray
cell.quantityLabel.font = cell.quantityLabel.font.withSize(14)
cell.productNameLabel.text = productsArray[indexPath.row]
cell.productNameLabel.font = UIFont.boldSystemFont(ofSize: 14)
如果你想使用单一责任原则,你能做的最好的事情就是不要让UITableView
的delegate
到你的UIViewController
并创建一个名为 YourTableViewController: UITableViewController 的新 class 并将其 class 分配给故事板中的 tableView。
现在您的 UIViewController
处理 UIViewController
问题,UITableView
class 处理 UITableView
问题,以及与 UITableViewCell
正在由 CustomUITableViewCell
class.
处理
所以最好是在您创建单元格之后调用 configurateWithItem()
函数,但是从您的 UITableView
自定义 class.
通过遵循此规则,您可以保持 UIViewControllers
小巧,并且不会创建 大型视图控制器 。 UITableViewControllers
也可以在不同的 UIViewControllers
.
中重复使用
答案可能基于个人意见,但我真的看不出有任何理由支持使用第二种 "standard version" 方法。
这超出了 MVC 的范畴,但您应该尽可能保持 public 接口封闭,只公开有意义的属性。所以大多数情况下使用private
。
所以我建议在你的小区里
@IBOutlet private var quantityLabel: UILabel!
@IBOutlet private var productNameLabel: UILabel!
你可以坚持weak
虽然没有必要。您还可以使用可选值而不是强制展开值 quantityLabel: UILabel?
而不是 quantityLabel: UILabel!
来提高稳定性。
因此,一旦您将内部部件设为私有,您就需要检查如何公开您的接口。这仍然有多种模式;一个是你使用的,你的细胞不保存你注入的对象,而只是修改它的视觉表示。另一个看起来像这样:
class NeedProductsTableViewCell: UITableViewCell {
@IBOutlet private var quantityLabel: UILabel?
@IBOutlet private var productNameLabel: UILabel?
var item: (quantity: String, name: String)? {
didSet {
if let item = item {
quantityLabel?.text = item.quantity
quantityLabel?.textColor = .systemGray
quantityLabel?.font = quantityLabel.font.withSize(14)
productNameLabel?.text = item.name
productNameLabel?.font = UIFont.boldSystemFont(ofSize: 14)
}
}
}
在这种情况下,您将在数据源中使用:
cell.item = (quantityProducts[indexPath.row], productsArray[indexPath.row])
这也表明您应该尽可能只使用 1 个数组。使用结构封装两个值或简单地使用元组:
private var items: [(quantity: String, name: String)] = []
而不是
private var quantityProducts: [String] = []
private var productsArray: [String] = []
我开始使用 MVC 编写我的第一个应用程序,但我不知道代码版本在这种模式下更好。
在viewcontroller中:
let quantity = quantityProducts[indexPath.row]
let nameProduct = productsArray[indexPath.row]
cell.configurateWithItem(quantity: quantity, name: nameProduct)
在细胞类中:
class NeedProductsTableViewCell: UITableViewCell {
@IBOutlet weak var quantityLabel: UILabel!
@IBOutlet weak var productNameLabel: UILabel!
func configurateWithItem(quantity:String,name:String){
quantityLabel.text = quantity
quantityLabel.textColor = .systemGray
quantityLabel.font = quantityLabel.font.withSize(14)
productNameLabel.text = name
productNameLabel.font = UIFont.boldSystemFont(ofSize: 14)
}
或者可能是标准版本(在视图控制器中设置单元格属性)?
let cell = tableView.dequeueReusableCell(withIdentifier: "NeedProductsTableViewCell", for: indexPath) as! NeedProductsTableViewCell
cell.quantityLabel.text = quantityProducts[indexPath.row]
cell.quantityLabel.textColor = .systemGray
cell.quantityLabel.font = cell.quantityLabel.font.withSize(14)
cell.productNameLabel.text = productsArray[indexPath.row]
cell.productNameLabel.font = UIFont.boldSystemFont(ofSize: 14)
如果你想使用单一责任原则,你能做的最好的事情就是不要让UITableView
的delegate
到你的UIViewController
并创建一个名为 YourTableViewController: UITableViewController 的新 class 并将其 class 分配给故事板中的 tableView。
现在您的 UIViewController
处理 UIViewController
问题,UITableView
class 处理 UITableView
问题,以及与 UITableViewCell
正在由 CustomUITableViewCell
class.
所以最好是在您创建单元格之后调用 configurateWithItem()
函数,但是从您的 UITableView
自定义 class.
通过遵循此规则,您可以保持 UIViewControllers
小巧,并且不会创建 大型视图控制器 。 UITableViewControllers
也可以在不同的 UIViewControllers
.
答案可能基于个人意见,但我真的看不出有任何理由支持使用第二种 "standard version" 方法。
这超出了 MVC 的范畴,但您应该尽可能保持 public 接口封闭,只公开有意义的属性。所以大多数情况下使用private
。
所以我建议在你的小区里
@IBOutlet private var quantityLabel: UILabel!
@IBOutlet private var productNameLabel: UILabel!
你可以坚持weak
虽然没有必要。您还可以使用可选值而不是强制展开值 quantityLabel: UILabel?
而不是 quantityLabel: UILabel!
来提高稳定性。
因此,一旦您将内部部件设为私有,您就需要检查如何公开您的接口。这仍然有多种模式;一个是你使用的,你的细胞不保存你注入的对象,而只是修改它的视觉表示。另一个看起来像这样:
class NeedProductsTableViewCell: UITableViewCell {
@IBOutlet private var quantityLabel: UILabel?
@IBOutlet private var productNameLabel: UILabel?
var item: (quantity: String, name: String)? {
didSet {
if let item = item {
quantityLabel?.text = item.quantity
quantityLabel?.textColor = .systemGray
quantityLabel?.font = quantityLabel.font.withSize(14)
productNameLabel?.text = item.name
productNameLabel?.font = UIFont.boldSystemFont(ofSize: 14)
}
}
}
在这种情况下,您将在数据源中使用:
cell.item = (quantityProducts[indexPath.row], productsArray[indexPath.row])
这也表明您应该尽可能只使用 1 个数组。使用结构封装两个值或简单地使用元组:
private var items: [(quantity: String, name: String)] = []
而不是
private var quantityProducts: [String] = []
private var productsArray: [String] = []