gpt4 book ai didi

rx-swift - 使用 RxDataSource 自动调整 collectionViewCells 的大小

转载 作者:行者123 更新时间:2023-12-02 21:39:02 24 4
gpt4 key购买 nike

如何使用RxDataSource实现自动调整collectionViewCells的大小?

我尝试过设置

flowLayout.estimatedItemSize = CGSize(width: 187, height: 102)

但是当 dataSourceObservable 更改时应用程序崩溃。

我尝试过在内部设置单元格大小

dataSource.configureCell = { [weak self] (dataSource, collectionView, indexPath, _) in 

这不是一个好主意,因为单元格重叠,这是因为我没有使用

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize

现在,是否可以仅使用可观测值来正确布局单元格大小?这不是调用类似的东西

dataSourceVar.value[indexPath].cellSize

collectionView sizeForItemAt里面?

最佳答案

遇到类似的问题,这样解决

// swiftlint:disable line_length type_name
final class RxCollectionViewSectionedReloadDataSourceAndDelegate<Section: SectionModelType>: RxCollectionViewSectionedReloadDataSource<Section>, UICollectionViewDelegateFlowLayout {
typealias CellSize = (CollectionViewSectionedDataSource<Section>, UICollectionView, UICollectionViewLayout, IndexPath, Item) -> CGSize
typealias SizeViewInSection = (CollectionViewSectionedDataSource<Section>, UICollectionView, UICollectionViewLayout, Int, Section) -> CGSize

private var cellSize: CellSize
private var headerSectionViewSize: SizeViewInSection?
private var footerSectionViewSize: SizeViewInSection?

init(
configureCell: @escaping ConfigureCell,
configureSupplementaryView: ConfigureSupplementaryView? = nil,
moveItem: @escaping MoveItem = { _, _, _ in () },
canMoveItemAtIndexPath: @escaping CanMoveItemAtIndexPath = { _, _ in false },
cellSize: @escaping CellSize,
headerSectionViewSize: SizeViewInSection? = nil,
footerSectionViewSize: SizeViewInSection? = nil
) {
self.cellSize = cellSize
self.headerSectionViewSize = headerSectionViewSize
self.footerSectionViewSize = footerSectionViewSize
super.init(
configureCell: configureCell,
configureSupplementaryView: configureSupplementaryView,
moveItem: moveItem,
canMoveItemAtIndexPath: canMoveItemAtIndexPath
)
}

override func collectionView(
_ collectionView: UICollectionView,
observedEvent: Event<RxCollectionViewSectionedReloadDataSource<Section>.Element>
) {
collectionView.delegate = self
super.collectionView(collectionView, observedEvent: observedEvent)
}

func collectionView(
_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath
) -> CGSize {
cellSize(self, collectionView, collectionViewLayout, indexPath, self[indexPath])
}

func collectionView(
_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
referenceSizeForHeaderInSection section: Int
) -> CGSize {
headerSectionViewSize?(self, collectionView, collectionViewLayout, section, sectionModels[section]) ?? .zero
}

func collectionView(
_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
referenceSizeForFooterInSection section: Int
) -> CGSize {
footerSectionViewSize?(self, collectionView, collectionViewLayout, section, sectionModels[section]) ?? .zero
}
}
// swiftlint:enable line_length type_name

关于rx-swift - 使用 RxDataSource 自动调整 collectionViewCells 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42588448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com