【发布时间】:2019-05-09 18:36:06
【问题描述】:
我正在使用 collectionView 创建一个提要应用程序并完成了大部分工作,但我有一些问题需要改进我的代码: 例如,我有一个这样的状态单元格:
通常,对于动态单元格内容,我将在委托 sizeForItemAt 中调整单元格的大小,如下所示:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
print(")-Layouting each cell")
let textView = UITextView()
textView.isScrollEnabled = false
textView.font = UIFont.preferredFont(forTextStyle: .subheadline)
textView.text = posts[indexPath.item].statusText
let estimatedSizeForStatus = textView.sizeThatFits(CGSize(width: view.frame.width, height: .infinity))
return CGSize(width: view.frame.width, height: 470 + estimatedSizeForStatus.height)
}
但我很难维护代码。当我想更改状态字体大小时,我必须替换 2 个位置的代码(在委托和单元类中)。
有没有更好的方法来改善这一点?
【问题讨论】:
标签: swift dynamic uicollectionviewcell