【发布时间】:2017-07-09 04:31:43
【问题描述】:
我有一个像这样的网格列表
class ListViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
private var collection: ACollection?
private var collectionViewLayout:UICollectionViewFlowLayout?
override func loadView() {
super.loadView()
collectionViewLayout = UICollectionViewFlowLayout()
collectionViewLayout?.itemSize.width = 128
collectionViewLayout?.itemSize.height = 227
view = UICollectionView(frame: CGRect.zero, collectionViewLayout: collectionViewLayout!)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let someObj: SomeObj = collection!.getObject(index: indexPath.item)
let cell: UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(UICollectionViewCell.self), for: indexPath as IndexPath)
let cellView: UIImageView = UIImageView(image: object.image)
cellView.frame = cell.bounds
cell.contentView.addSubview(cellView)
return cell
}
我希望我的单元格具有不同的纵横比,尽管取决于 someObj 中的值。如何在不重叠的情况下调整单元格的大小?我试着做
cellView.frame = CGRect(x:0,y:0,width:100,height:300)
cellView.frame =cell.bounds
但单元格重叠。
【问题讨论】:
-
我认为你应该在
UICollectionViewDelegateFlowLayout中实现collectionView(, layout: sizeForItemAt:)函数,因为看起来你正在使用流布局。 -
@sCha 有点用。我看到的是,当我更改大小时,它会使用旧尺寸绘制一个单元格,然后在顶部绘制一个新尺寸的单元格
标签: iphone swift layout autolayout