我是一个相当新的开发人员,我有一些长的cellForItemAt方法。我觉得我错过了一些重要的事情。
在这个ViewController中,我有一个分段控件,它用布尔taskView
属性过滤数据。
以下是我的cellForItemAt调用的样子:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TaskCell.reuseIdentifier, for: indexPath) as! TaskCell
//SwipeCell Delegate
cell.delegate = self
var transactionResults: Results<Transaction>
if taskView {
transactionResults = unclearedTransactionsToDate
} else {
transactionResults = allTransactions
}
cell.configureCollectionViewCells(indexPath, transactionResults)
let balanceAtDate: Double = realm.objects(Transaction.self).filter("transactionDate <= %@", transactionResults[indexPath.item].transactionDate).sum(ofProperty: "transactionAmount")
cell.balanceLabel.attributedText = balanceAtDate.toAttributedString(size: 9, offset: 6)
if transactionResults[indexPath.item].isCleared == false && !taskView {
cell.amountLabel.textColor = .lightGray
cell.subcategoryLabel.textColor = .lightGray
cell.dateLabel.textColor = .lightGray
cell.balanceLabel.textColor = .lightGray
cell.circleView.backgroundColor = .lightGray
} else {
cell.subcategoryLabel.textColor = .black
cell.dateLabel.textColor = .black
cell.balanceLabel.textColor = .black
cell.circleView.backgroundColor = UIColor(rgb: transactionResults[indexPath.item].transactionCategory!.categoryColor)
}
return cell
}
}
在我的configureCollectionViewCells
方法中,我有:
func configureCollectionViewCells(_ indexPath: IndexPath, _ transaction: Results<Transaction>) {
imageView.image = UIImage(named: transaction[indexPath.item].transactionCategory!.categoryName)
imageView.tintColor = .white
circleView.backgroundColor = UIColor(rgb: transaction[indexPath.item].transactionCategory!.categoryColor)
subcategoryLabel.textColor = .black
dateLabel.textColor = .black
balanceLabel.textColor = .black
subcategoryLabel.text = transaction[indexPath.item].transactionSubCategory?.subCategoryName
amountLabel.attributedText = transaction[indexPath.item].transactionAmount.toAttributedString(size: 9, offset: 6)
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd, yyyy"
let dateString = formatter.string(from: transaction[indexPath.item].transactionDate)
dateLabel.text = dateString
if transaction[indexPath.item].transactionAmount > 0 {
amountLabel.textColor = UIColor(rgb: Constants.green)
} else {
amountLabel.textColor = UIColor(rgb: Constants.red)
}
}
代码是有效的,但我感觉它没有得到正确的实现。有人能给我一些关于如何管理如此冗长的函数的建议吗(记住我是编程新手(?我觉得我错过了几个概念。
有些人刚刚说";将所有内容放入cellForItemAt";有些在cellForItemAt中只有3行。
我想也许我应该重写collectionView单元格中的layoutSubviews
方法,并在那里实现一些代码。
我们非常感谢任何一般或具体的建议。如果有人有任何关于这个话题的资源,我也会感兴趣进行研究。
提前谢谢。
你正在做的不是"错误的";本身。然而,有一种学派认为,理想情况下,cellForRowAt
应该对细胞的内部界面一无所知。这(cellForRowAt
(是数据源。它应该只将数据交给单元格。你有一个单元格子类(TaskCell(,所以它只需要一些方法或属性,就可以告诉它数据是什么,然后单元格应该根据这些设置设置自己的格式并填充自己的界面。
如果所有的格式化和配置代码都移到单元格子类中,那么cellForRowAt
的实现将更短、更干净、更清晰,分工也将更合适。
为了支持这一理念,我只想补充一点,苹果已经在iOS 14中采用了它,在那里,一个单元现在可以有一个UIContentConfiguration对象,其工作是将数据从cellForRowAt
传输到单元的contentView
。例如,不要说(对于表视图单元格(cell.textLabel.text = "howdy"
,而是说configuration.text = "howdy"
,并让配置对象担心接口中可能涉及UILabel这一事实