【发布时间】:2019-07-05 10:16:45
【问题描述】:
我的集合视图(网格格式)单元格高度有问题。网格格式总共包含两列,所以基本上每行有 2 个单元格。我的集合视图单元格高度根据其中的内容而增加,但内容是中心对齐而不是顶部对齐。我想在具有更大高度的行中实现单元格的高度。我怎样才能做到这一点?请指教。
https://drive.google.com/file/d/0B56cE4V6JI-RYmdQT2pIZ0hYQ2phM3Z2YmJNYU1SeXNnYTNN/view?usp=sharing https://drive.google.com/file/d/0B56cE4V6JI-RQUdqVmV4b244cFI5SGd2TnJfbG1tckdQU21Z/view?usp=sharing https://drive.google.com/file/d/0B56cE4V6JI-RYTYxVzJyVUp1clpJTkVqYjN6QXBPeERvVHZR/view?usp=sharing
我在上面添加了一个链接,这是现在的问题。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let cellsAcross: CGFloat = 2
let spaceBetweenCells: CGFloat = 10
let dim = (collectionView.bounds.width - (cellsAcross - 1) * spaceBetweenCells) / cellsAcross
print(dim) // calculating width
//calculating my content height
let toppingHeight = sizeOfString(string: PizzaMenuItems[indexPath.row].pizzaToppings, constrainedToWidth: Double(dim))
let pizzaNameHeight = sizeOfString(string: PizzaMenuItems[indexPath.row].menuName, constrainedToWidth: Double(dim))
let newHeight = toppingHeight.height + pizzaNameHeight.height + 57
// to increase my collection view height
let heightt = (view.frame.width)/2
let count = self.PizzaMenuItems.count
if self.PizzaMenuItems.count == 1 {
}
else{
if count % 2 == 0
{ //even Number
collectionViewC_Height.constant = heightt * CGFloat(self.PizzaMenuItems.count/2) + 57
} else
{ // odd Number
collectionViewC_Height.constant = heightt * CGFloat(self.PizzaMenuItems.count/2+1)}
}
return CGSize(width: dim, height: newHeight)}
【问题讨论】:
-
您可以预先计算每个单元格的所有高度,并根据赔率或现在检查第+1行或第-1行,并使用最大值作为单元格的高度?
标签: swift iphone xcode uicollectionview uicollectionviewcell