gpt4 book ai didi

ios - 如何在内部将模型数据发送到单元格?

转载 作者:行者123 更新时间:2023-11-28 13:36:46 25 4
gpt4 key购买 nike

我有以下代码在 TableView 中显示数据。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? ArticleTalbeViewCell else {
fatalError("ArticleTableViewCell not found")
}

let articleVM = self.articleListVM.articleAtIndex(indexPath.row)
// cell.viewModel = articleVM
cell.titlelabel?.text = articleVM.title
cell.descriptionLabel?.text = articleVM.description
return cell
}
}

现在,我的代码用

cell.titlelabel?.text = articleVM.title
cell.descriptionLabel?.text = articleVM.description

工作顺利。

cell.viewModel = articleVM 是一种好的做法吗?

假设我必须多次为单元格设置日期?这种方法 cell.viewModel = articleVM 将节省几行。

UITableViewCell 的代码如下:

class ArticleTalbeViewCell: UITableViewCell {

var titlelabel:UILabel?
var descriptionLabel:UILabel?

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.setupUI()

}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
fatalError("init(coder:) has not been implemented")
}

private func setupUI() -> () {
let label1 = UILabel()
label1.numberOfLines = 0
let label2 = UILabel()
label2.numberOfLines = 0
label2.textColor = UIColor.lightGray
label2.setContentHuggingPriority(UILayoutPriority.defaultHigh, for: NSLayoutConstraint.Axis.vertical)

titlelabel = label1
descriptionLabel = label2
let staview = UIStackView()
staview.axis = .vertical

staview.addArrangedSubview(label1)
staview.addArrangedSubview(label2)
staview.spacing = 8
staview.translatesAutoresizingMaskIntoConstraints = false // !important
self.contentView.addSubview(staview)

staview.topAnchor.constraint(equalTo: self.contentView.topAnchor,constant: 5).isActive = true
staview.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -5).isActive = true
staview.leftAnchor.constraint(equalTo: self.contentView.leftAnchor, constant: 5).isActive = true
staview.rightAnchor.constraint(equalTo: self.contentView.rightAnchor, constant: -5).isActive = true
}
}

ArticleListViewModel 代码如下:

struct ArticleListViewModel {
let articles:[Article]
}

extension ArticleListViewModel {

var numbeOfSections: Int {
return 1
}

func numOfRowsInSection(_ section: Int) -> Int {
return self.articles.count
}

func articleAtIndex(_ index: Int) -> ArticleViewModel {
let article = self.articles[index]
return ArticleViewModel(article)
}
}

struct ArticleViewModel {
private let article: Article
}

extension ArticleViewModel {
init(_ article: Article) {
self.article = article
}
}

extension ArticleViewModel {
var title: String {
return self.article.title ?? "null"
}

var description: String {
return self.article.description ?? "null"
}
}

ArticleList代码如下:

import Foundation

struct ArticleList: Decodable {
let articles: [Article]
}

struct Article: Decodable {
let title: String?
let description: String?
let chines: String?
}

如何编辑“cell”代码来实现cell.viewModel = articleVM

最佳答案

首先,我不认为给予 cell.viewModel = articleVM 不会改变太多东西。因为您已经创建了 ArticleListViewModelArticleViewModel,所以如果您为表格 View 单元格创建这些虚拟机也是一样的。但是,我可以给你一些我在使用 MVVM 方法时使用的建议。您可以在表格 View 单元格内添加新属性,然后使用属性观察器为其赋值。

cell.article = articleVM

因此,您的 cellForRowAt 方法不会太长,它几乎与将 View 模型提供给单元格一样。

private var titlelabel:UILabel?
private var descriptionLabel:UILabel?

var article: ArticleViewModel? {
didSet {
guard let article = article else { return }
titlelabel?.text = article.title
descriptionLabel?.text = article.description
}
}

据我了解,您的第二个问题是您是否在 cellForRowAt 方法中使用了日期变量和格式,占用太多空间并且看起来很丑陋。这就是为什么您需要在 View 模型中执行业务逻辑。您的表格 View 单元格只负责显示它而不是格式化它。我希望,这对你来说很清楚。如果您有任何问题,可以提出。

关于ios - 如何在内部将模型数据发送到单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56511026/

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