大小細胞有gridlist看起來像這樣我如何在GridView中
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
@sCha有點作用。我看到的是,當我改變尺寸時,它繪製一箇舊尺寸的單元格,然後再用新尺寸繪製另一個單元格 – user2175783