【发布时间】:2019-02-15 16:06:27
【问题描述】:
我通过 switch 函数输出 4 个自定义单元格,现在我需要为每个单元格输出动态高度值或固定高度值。我都喜欢,因为它们的高度始终是静态的。
我订阅了uiСollectionviewВelegateFlowLayout 协议并找到了如何更改所有单元格的高度
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = CGSize(width: 357, height: 600)
return size
要覆盖单个单元格的高度,我使用该函数
cell.frame = CGRect(cell.frame.origin.x, cell.frame.origin.y, width: 100.0, height: 100.0)
但它不起作用,“表达式类型 '@lvalue CGRect' 在没有更多上下文的情况下是模棱两可的”
在这种情况下我该怎么办?用什么函数?
完整代码如下
// main protocols UICollectionView
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
// width and height for cell in collectionView
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = CGSize(width: 357, height: 600)
return size
}
//margins for cell in collectionView
func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 40.0
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell: UICollectionViewCell!
switch indexPath.row {
case 1:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath)
as? secondCollectionViewCell
cell.frame = CGRect(cell.frame.origin.x, cell.frame.origin.y, width: 100.0, height: 100.0)
case 2:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "thirdCell", for: indexPath)
as? cellThirdCollectionViewCell
case 3:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "fourthCell", for: indexPath)
as? fourthCollectionViewCell
default:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath)
as? imageModelCollectionViewCell
}
return cell
}
}
【问题讨论】:
标签: swift uicollectionview uicollectionviewcell cgrect