【问题标题】:Leading TableView to MVC in Swift在 Swift 中将 TableView 引导到 MVC
【发布时间】:2021-05-01 16:13:30
【问题描述】:

现在我得到这样的单元格的所有数据:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TVCellTableViewCell
    
    cell.comeTypeLabel.text = "Writing off"
    cell.amountLabelCell.text =  String(RealmModel.shared.getSections()[indexPath.section].items[indexPath.row].Amount)
    if RealmModel.shared.getSections()[indexPath.section].items[indexPath.row].category != "" {
        cell.labelCell.text = RealmModel.shared.getSections()[indexPath.section].items[indexPath.row].category
    } else {
        cell.labelCell.text = "Income"
        cell.comeTypeLabel.text = ""
    }
    return cell
}

它有效,但我需要将我的项目引入 MVC。所以,据我所知,我需要在单元类中编写所有逻辑:

class TVCellTableViewCell: UITableViewCell {
@IBOutlet weak var labelCell: UILabel!
@IBOutlet weak var amountLabelCell: UILabel!
@IBOutlet weak var comeTypeLabel: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    labelCell.font = UIFont(name: "Gill Sans SemiBold", size: 20)
    labelCell.textColor = UIColor.black
    amountLabelCell.font = UIFont(name: "Gill Sans SemiBold", size: 20)
    amountLabelCell.textColor = UIColor.black
    comeTypeLabel.font = UIFont(name: "Gill Sans SemiBold", size: 18)
    comeTypeLabel.textColor = UIColor.gray
    
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}

}

但我不知道该怎么做,因为我使用的“indexpathes”和其他词只允许使用 im tableView 的功能。 有人可以告诉我,如何做正确并导致 MVC,请

【问题讨论】:

  • 您的第一个 sn-p 代码非常典型。您可以稍微重构一次以访问领域模型并将结果存储在局部变量中,但其他方面都很好。另一种方法是在您从cellForRow 调用的单元类中创建一个函数。您可以将领域对象传递给此函数,以便单元格可以设置其内容。
  • 你也应该小心使用灰色和黑色等颜色;当设备切换到暗模式时,您将遇到问题。您可以使用自适应颜色,例如 .labeltertiaryLabel
  • @Paulw11,你能告诉更多关于单元类中的函数,甚至更好的代码示例,因为我不明白如何在 tableView 的函数之外提供数据。
  • 例如:labelCell.text = RealmModel.shared.getSections()
  • 然后呢?我不能使用索引路径

标签: ios swift uitableview model-view-controller


【解决方案1】:

您的第一个 sn-p 并没有什么问题,只是在局部变量中捕获 RealmModel.shared.getSections()[indexPath.section].items[indexPath.row] 可能比执行两次索引更好。

如果您想将代码移动到您的单元格,您可以将领域对象传递给单元格类中的函数。你还没有提供 Realm 模型对象的类型,所以我将使用Item。你会有类似的东西:

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

    let item = RealmModel.shared.getSections()[indexPath.section].items[indexPath.row]
    cell.configure(with: item)
     
    return cell
}

class TVCellTableViewCell: UITableViewCell {

//... existing code
    func configure(with item:Item) {
        if item.category.isEmpty {
            self.comeTypeLabel.text = ""
            self.labelCell.text = "Income"
        } else {
            self.comeTypeLabel.text = "Writing off"
            self.labelCell.text = item.category
        }
        self.amountLabelCell.text = String(item.amount)
     }
}

请注意,按照惯例,amount 等属性名称应以小写字母开头,我已在代码中进行了此更改。

【讨论】:

    猜你喜欢
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多