为什么我不能使用来自UICollectionViewFlowLayout的referenceSizeForFooterInSection和EstimateItemSize?

时间:2017-05-05 21:32:28

标签: 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)
}
}

感谢。

1 个答案:

答案 0 :(得分:0)

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