ios - swift 4在同一个UICollectionView垂直流上设置不同高度和宽度的单元格
问题描述
我尝试了下面的代码
import UIKit
class ViewController: UIViewController,UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionViw: UICollectionView!
var widthss = [59.0,59.0,59.0,121.0,59.0,59.0,59.0,59.0,59.0,59.0,28.0,28.0,59.0,59.0,] as [CGFloat]
var heights = [36.0,36.0,36.0,76.0,36.0,76.0,36.0,36.0,36.0,76.0,36.0,36.0,36.0,76.0,] as [CGFloat]
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return heights.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! UICustomCollectionViewCell
// Customize cell height
cell.frame = CGRect(x: cell.frame.origin.x, y: cell.frame.origin.y , width: widthss[indexPath.row], height: heights[indexPath.row])
return cell
}
}
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: widthss[indexPath.row], height: heights[indexPath.row])
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}}
我需要的是使集合视图单元格像第一张图片任何人都知道我的错误在哪里提前谢谢
解决方案
推荐阅读
- java - 使用相同的转义和引号字符分隔 CSV
- python-3.x - 设计卷积自动编码器的问题
- quarkus - 在@ApplicationScoped bean 中注入实体管理器
- c# - 标签具有巨大的行距,导致在 wpf 中相互重叠
- python - 如何在用户输入后使用熊猫打开 .csv 文件?
- angular - 如何使用路由器更改角度路径中的路径变量,以便 dogs/1/collar 可以成为 dogs/2/collar
- asp.net - 未经用户同意继续(Azure WebApp)
- module - 如何在开发过程中设置 %?RESOURCES 键?
- elasticsearch - 从输出中删除查询短语以进行词干查询弹性搜索
- c# - 如何包含同一个表中的多个表?