模型不应该与 交谈view
,但到目前为止,我一直看到以这种方式工作的项目:
1-Controller
从数据库创建模型对象,将其存储在某处(数组、字典、变量)
2-相应Controller
地更新view
,即使在 collectionViews 中使用委托方法 fi。
我的问题是:模型是否需要完美地表示视图将要显示的内容?这样我就可以将模型对象传递给视图并根据它设置视图?
到目前为止,我一直在执行此过程,但我想知道这种方法是否有问题。
要清楚的示例,在controller
:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! PostCollectionviewCell
cell.currentItem = self.posts[indexPath.item] as? Post
return cell
}
在view
使用中property observer
:
var currentItem :Post?{
didSet{
guard let thumbnailUrlString = self.currentItem?.thumbnailUrl else { return }
self.imageview.sd_setImage(with: URL(string: thumbnailUrlString))
bottomLayer.isHidden = true
}
}