如何解决如何在集合视图中显示正确的部分项?
如果项目说明与部分说明匹配,我想在部分中显示项目。
这是商品的数据模型:
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 举报,一经查实,本站将立刻删除。