设置单元格的更好方法

问题描述 投票:0回答:1

[我开始使用MVC编写我的第一个应用程序,但我不知道在这种模式下使用代码版本会更好。

在viewcontroller中:

 let quantity = quantityProducts[indexPath.row]
 let nameProduct = productsArray[indexPath.row]
 cell.configurateWithItem(quantity: quantity, name: nameProduct)

在cellClass中:

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)
ios swift xcode tableview cell
1个回答
0
投票

最好使用

func configurateWithItem(quantity:String,name:String){

因为它封装了应该与vc分开的单元配置逻辑

© www.soinside.com 2019 - 2024. All rights reserved.