如何正确管理精心制作的Collection View cellForItemAt方法?

问题描述

我是一个相当新的开发人员,我有一些很长的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方法,并在那里实现一些代码。

任何一般或特定建议,我们将不胜感激。我也将有兴趣研究是否有人对此主题有任何资源。

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)