如何在集合视图中显示正确的部分项?

问题描述

如果项目说明与部分说明匹配,我想在部分中显示项目。

这是商品的数据模型:

import Foundation
import RealmSwift

class Transaction: Object {
    
    @objc dynamic var transactionDescription: String? = nil
    @objc dynamic var transactionAmount = 0.0
    @objc dynamic var transactionDate = Date()
    @objc dynamic var transactionCategory: Category? = nil
    @objc dynamic var repeatInterval = ""
    @objc dynamic var isCleared = false
    @objc dynamic var subCategoryName = ""
    @objc dynamic var transactionID = UUID().uuidString
    
    convenience init(theDate: Date) {
        self.init()
        self.transactionDate = theDate
    }
    
    override static func primaryKey() -> String? {
        return "transactionID"
    }
        
}

这是我使用的部分数量模型:

class SubCategory: Object {
    
    @objc dynamic var subCategoryName = ""
    
}

这是我当前的代码


lazy var subCategories = List<SubCategory>()

lazy var transactions: Results<Transaction> = { self.realm.objects(Transaction.self) }()

var categorySelected: Category?


func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        
        return transactions.count
        
    }

func numberOfSections(in collectionView: UICollectionView) -> Int {
        return categorySelected?.subCategories.count ?? 0
    }

func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let transactionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Subcell",for: indexPath) as! Subcell
        
        if transactions[indexPath.item].subCategoryName == subCategories[indexPath.section].subCategoryName {
            
            transactionCell.nameLabel.text = transactions[indexPath.item].transactionDescription

            transactionCell.dateLabel.text = transactions[indexPath.item].transactionDate.toString()

            transactionCell.amountLabel.text = transactions[indexPath.item].transactionAmount.toCurrency()

        return transactionCell
}

代码显示了正确的部分数量,但每个部分的每个部分都显示了相同数量的项目,并且在不应该存在任何幻影占位符的项目中。

我如何只显示每个部分的正确项目数?

我尝试过,但是没有给我正确的结果。

func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        
        return transactions[section].subCategoryName.count
        
    }

谢谢!

解决方法

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

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

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