【发布时间】:2020-06-16 05:49:18
【问题描述】:
所以我使用组合布局来自动调整单元格的大小。这就是我创建布局部分的方式。然后我在 UICollectionViewCompositionalLayout(sectionProvider:) 中返回它。
}
private func whatsNewSection() -> NSCollectionLayoutSection {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(80))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let group = NSCollectionLayoutGroup.vertical(layoutSize: itemSize, subitems: [item])
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 24, bottom: 24, trailing: 24)
section.boundarySupplementaryItems = [
.init(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(50)), elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
]
return section
}
所以这个部分看起来像这样。
现在我有 2 个 nib 文件,每个文件都有自己的类。部分标题只有一个标签和一个按钮。让我们忽略标题,因为问题出在 WhatsNewCell 上。类文件看起来像这样。
class WhatsNewCell: UICollectionViewCell {
static let reuseIdentifier = String(describing: WhatsNewCell.self)
var delegate: WhatsNewCellDelegate?
@IBOutlet weak var appVersionLabel: UILabel!
@IBOutlet weak var appUpdateLabel: UILabel!
@IBOutlet weak var moreButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
moreButton.addTarget(self, action: #selector(handleMore), for: .touchUpInside)
}
@objc private func handleMore() {
self.moreButton.setTitle("", for: .normal)
self.appUpdateLabel.numberOfLines = 0
delegate?.moreButtonClick()
}
}
好的,版本标签和更新标签在堆栈视图中。当用户单击按钮时,我更改更新标签的行数。现在我创建了一个名为 WhatsNewCellDelegate 的委托,以便能够从我的控制器重新加载我的集合视图。这就是我在控制器中重新加载集合视图的方式。当我初始化一个单元格时,我确实将单元格委托设置为我的控制器。
extension AppDetailCollectionVC: WhatsNewCellDelegate {
func moreButtonClick() {
//collectionView.collectionViewLayout.invalidateLayout()
collectionView.performBatchUpdates({
//collectionView.reloadData()
collectionView.reloadSections(IndexSet(integer: 2))
}, completion: nil)
}
}
现在当我调用重新加载数据时它可以工作,但当我调用重新加载部分时它不起作用。我必须按两次更多按钮才能使用重新加载部分。我也尝试使用 invalidateLayout() ,但它使我的一些单元格消失了。看看下面的屏幕截图。
这是当我使用 invalidateLayout() 单击更多按钮时的样子。
所以我不知道发生了什么。我如何能够重新加载该部分而不必按两次更多按钮。非常感谢您的帮助。
【问题讨论】:
-
简单地说 - 您的单元格存储的信息在另一个单元格出列时不会持久保存。此外,它不会被重置。您必须将有关“更多”的信息存储在您的控制器中,并始终在您的单元格上更新它。