【问题标题】:CollectionView Height IssueCollectionView 高度问题
【发布时间】:2018-03-23 10:03:59
【问题描述】:

如何增加单元格高度。此外,我正在增加情节提要的单元格高度,但它不起作用。 注意:(只有我想增加什么是新单元格高度)

代码:-

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

    if let flowLayout = collectionview.collectionViewLayout as? UICollectionViewFlowLayout {
        let horizontalSpacing = flowLayout.scrollDirection == .vertical ? flowLayout.minimumInteritemSpacing : flowLayout.minimumLineSpacing
        let cellWidth = (view.frame.width - max(0, numberOfCellsPerRow - 1)*horizontalSpacing)/numberOfCellsPerRow

        flowLayout.itemSize = CGSize(width: cellWidth, height: cellWidth)
    }


    return CGSize(width: (self.view.frame.width - 8) / 3.0 , height: (self.view.frame.width - 8) / 3.0)
}

【问题讨论】:

  • 确保你使用了UICollectionViewDelegateFlowLayout这个和类名。
  • 这是一个集合视图,还是三个集合视图?
  • @MilanNosáľ 在这个页面中这些有三个 collectionViews

标签: ios swift uicollectionview


【解决方案1】:

sizeForItemAt 会为您集合中的每个项目调用。这段代码:

if let flowLayout = collectionview.collectionViewLayout as? UICollectionViewFlowLayout {
    let horizontalSpacing = flowLayout.scrollDirection == .vertical ? flowLayout.minimumInteritemSpacing : flowLayout.minimumLineSpacing
    let cellWidth = (view.frame.width - max(0, numberOfCellsPerRow - 1)*horizontalSpacing)/numberOfCellsPerRow

    flowLayout.itemSize = CGSize(width: cellWidth, height: cellWidth)
}

看起来它属于viewDidLoad,或者其他地方只会被调用一次。此外,如果我没记错的话,设置 flowLayout.itemSize 在所有项目的大小相同时使用 - 因此它不能与 sizeForItemAt 实现结合使用(换句话说,我会完全删除它。

这是最终应用的内容:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: (self.view.frame.width - 8) / 3.0 , height: (self.view.frame.width - 8) / 3.0)
}

所以要更改给定collectionView 中单元格的高度,只需更改返回的CGSize 中的高度,例如,更改为collectionView.frame.height 以适应collectionView 的高度:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: (self.view.frame.width - 8) / 3.0 , height: collectionView.frame.height)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 2011-01-25
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    相关资源
    最近更新 更多