Swift 如何在Ios中为UICollectionView设置自定义布局
我知道如何在UICollectionView中显示集合单元格。但我想使用自定义布局来显示单元格 我的收藏应该有两列,其中单元格的宽度是固定的,但单元格的高度可能因标签的内容大小而异 但我不知道怎么做。 我想显示单元格集合,如下图所示Swift 如何在Ios中为UICollectionView设置自定义布局,swift,Swift,我知道如何在UICollectionView中显示集合单元格。但我想使用自定义布局来显示单元格 我的收藏应该有两列,其中单元格的宽度是固定的,但单元格的高度可能因标签的内容大小而异 但我不知道怎么做。 我想显示单元格集合,如下图所示 我想用swift实现它。使用此功能,您可以根据需要更改单元格的高度和宽度 func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayo
我想用swift实现它。使用此功能,您可以根据需要更改单元格的高度和宽度 func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,SizeFormiteIndeXPath indexPath:indexPath)->CGSize{}
使用indexPath访问单元格使用此函数可以根据需要更改单元格的高度和宽度 func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,SizeFormiteIndeXPath indexPath:indexPath)->CGSize{} 使用indexPath访问单元格
以下是自定义布局的代码。在此布局中,单元格宽度为集合视图的一半,高度为每个单元格的特定高度。要设置每个单元格的高度,必须实现“func heightFor(index:Int)->CGFloat”。 要实现此布局,请执行以下步骤
//This protocol has to be confirmed in your controller.
protocol CustomLayoutDelegate {
//This method accept the height for your cell.
func heightFor(index : Int) -> CGFloat
}
以下是自定义布局的代码。在此布局中,单元格宽度为集合视图的一半,高度为每个单元格的特定高度。要设置每个单元格的高度,必须实现“func heightFor(index:Int)->CGFloat”。 要实现此布局,请执行以下步骤
//This protocol has to be confirmed in your controller.
protocol CustomLayoutDelegate {
//This method accept the height for your cell.
func heightFor(index : Int) -> CGFloat
}
谢谢,我按照您提到的那样做了,看起来像我期望的那样,但现在的问题是,当我要删除特定单元格(删除特定数组元素并重新加载collectionView)时,应用程序就会崩溃。它会显示类似“UICollectionView接收到索引路径不存在的单元格的布局属性”这样的错误:{length=2,path=0-4}'我一直在用这种方法编写我的应用程序。删除任何单元格后,应用程序不会崩溃。也许你可以禁用使用属性缓存数组进行测试。谢谢,我按照你提到的做了,看起来像我期望的,但现在的问题是,当我要删除特定单元格时(删除特定数组元素并重新加载collectionView)然后应用程序崩溃
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
//Register the cell for collection view
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
//Assign the delegate
if let layout = collectionView.collectionViewLayout as? CustomLayout{
layout.layoutDelegate = self
}
}
//CustomLayoutDelegate method
func heightFor(index: Int) -> CGFloat {
//Implement your own logic to return the height for specific cell
return CGFloat(max(1, index) * 50)
}
// UICollectionViewDataSource methods
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
cell.backgroundColor = UIColor.red
return cell
}
}