首页 > 解决方案 > 我将如何显示多个视频、图像?当我上传多张图片和视频时

问题描述

我正在尝试使用 UICollectionView 来显示多个图像,例如我的屏幕截图。任何人都可以建议我如何管理这个设计。

像这样

集合视图示例图像

标签: iosswiftuicollectionview

解决方案


您可以使用 SizeForItemAt 函数为每个单元格设置自定义大小,并在最后一个单元格内添加一个按钮以打开最后两个图像,或使用 didSelectItemAt 打开最后一个单元格。

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
      var collectionViewSize = collectionView.frame.size
   if indexPath.row == 0 {
      collectionViewSize.width = collectionViewSize.width/2.0 //Display 2 items in a row.
      collectionViewSize.height = collectionViewSize.height/2.0
     }
 if indexPath.row == 1 {

  collectionViewSize.width = collectionViewSize.width/3.0 //Display 3 items in row
  collectionViewSize.height = collectionViewSize.height/2.0
  }
      return collectionViewSize
}





 func collectionView(_ collectionView: UICollectionView,    numberOfItemsInSection section: Int) -> Int {
     return yourImageArray.count
}



   func collectionView(_ collectionView: UICollectionView, cellForItemAt    indexPath:  IndexPath) -> UICollectionViewCell {
 let cell =  collectionView.dequeueReusableCell(withReuseIdentifier: "YourCustomCell", for: indexPath) as! YourCustomCell 
cell.imageView.image = yourImageArray[indexPath.item] as UIImage
return cell 
}

*记得在你的 ViewController 中设置 UICollectionViewDelegateFlowLayout


推荐阅读