ios - 为什么我不能将 UICollectionViewFlowLayout 中的 referenceSizeForFooterInSection 与 EstimateItemSize 一起使用?

标签 ios swift uicollectionview uicollectionviewlayout

我正在尝试制作一个具有自调整大小和无限滚动功能的collectionView,但是当我设置referenceSizeForFooterInSection方法时,我的单元格没有显示。

当我删除estimateItemSize 属性并设置ItemSize 属性时,将显示单元格。

我的代码有什么问题?

我的VC:

override func viewDidLoad() {
    super.viewDidLoad()

    // Set explorerTableView
    setupExplorerCollectionView()

    // load presenter viewDidLoad
    presenter.viewDidLoad()
}


func setupExplorerCollectionView() {
    // Set flow layout
    if let flowLayout = explorerCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        flowLayout.estimatedItemSize   = CGSize(width: self.view.frame.width, height: 398)
        flowLayout.minimumLineSpacing  = 0
        flowLayout.scrollDirection     = .vertical
    }


    // Set delegate & dataSource
    explorerCollectionView.dataSource = self
    explorerCollectionView.delegate   = self

    // Register cells
    explorerCollectionView.register(ExplorerCell.self)
    explorerCollectionView.registerView(LoadingCell.self, for: UICollectionElementKindSectionFooter)
}

extension ExplorerViewController: ExplorerView {

func loadExplorerData(_ stories: [Story]) {
    self.stories.append(contentsOf: stories)
    self.explorerCollectionView.reloadData()
}

func showNoContentView() {
    //
}

extension ExplorerViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if indexPath.item == stories.count - 1 {
        if loadMore == .haveMore {

        }
    }

    let cell = collectionView.dequeueReusableCell(for: indexPath) as ExplorerCell
    cell.setup(with: stories[indexPath.item])
    return cell
}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    var footer: LoadingCell!

    if (kind ==  UICollectionElementKindSectionFooter) && (loadMore != .finished){
        footer = collectionView.dequeueReusableView(of: kind, for: indexPath) as LoadingCell
        footer.setup()
    }

    return footer
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
    return (loadMore == .finished) ? CGSize.zero : CGSize(width: self.view.frame.width, height: 50)
}
}

谢谢。

最佳答案

我最终使用collectionView(_:layout:sizeForItemAt:)来计算每个单元格的内容大小。

关于ios - 为什么我不能将 UICollectionViewFlowLayout 中的 referenceSizeForFooterInSection 与 EstimateItemSize 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43814026/

相关文章:

ios - CollectionView 高度问题

ios - 使用 XCTestCase 将项目插入到 Collection View 失败

ios - 日期未保存到核心数据对象

ios - (Swift)当多个 View Controller 导致相同 View 时放松 segue?

ios - 我怎样才能在 UILabel 的上部文本下面开始这个剩余文本?

swift - 根据字段名称过滤对象数组

ios - 在 iOS 中暂停和恢复定时器

ios - 如何在swift 3中使用addTarget方法

ios - 我什么时候应该构建支持 armv7s 的应用程序?

ios - UICollectionViewLayout 子类中未调用 layoutAttributesClass