gpt4 book ai didi

ios - 如何确定我需要为 UICollectionView 设置的高度?

转载 作者:行者123 更新时间:2023-11-29 01:11:22 25 4
gpt4 key购买 nike

如何根据其中的单元格数量以编程方式设置 UICollectionView 的高度?

到目前为止,这是我的想法,

    CGFloat cellHeight = 100;
CGFloat height = [imageFilesArray count] * cellHeight;
CGFloat width = self.view.bounds.size.width;
collectionView.frame = CGRectMake(0, 177, width, height);

此外,当我这样做时, Collection View 的下半部分没有响应 didSelect

最佳答案

您可以根据要显示的项目数计算 Collection View 的高度。这是一个计算 Collection View 高度的小函数。

此方法显示一行中的两个元素。您可以根据需要进行修改。

func calculateCollectionViewHeight(imageList:[AnyObject]) -> CGFloat {
if imageList.count <= 2 {
return 320 // if will set height to 320 if there is only two or less than 2 element in the list
}
if products.count % 2 == 0 {
return CGFloat(295 * (products.count / 2)) // this will calculate the height of collection view if list is even
}else{
return CGFloat(295 * (products.count / 2 + 1)) // this will calculate the height of collection view if list is odd
}

}

现在打电话

collectionView.frame.size.height = calculateCollectionViewHeight(tempArray) // list of objects

关于ios - 如何确定我需要为 UICollectionView 设置的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35710010/

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