UICollectionViewCellの寸法を動的にサイズ変更する最善の方法を知りたい(Swift 3)。私は、UICollectionViewのための適切なプロトコルを実装するViewControllerへの非同期の画像のアップロードを持っています。イメージはすべて異なるサイズになるので、UIImageViewデータをロードした後にセルの寸法を設定する必要があります。私はcollectionview自体のレイアウトのオプションを公開していますが、最初にセルの寸法を正確にしたいと思います。UICollectionViewCellを画像アップロードで動的にサイジングするSwift
すでに類似している投稿がいくつかありますが、私はSwift 3でこれを行う最も適切な方法を知りたいと思いますし、まだ正しい方法を見つけることができません。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {}
メソッドで動的にサイズ変更を行うことはできますか?または、ビュー全体をロードするときに設定できる自動サイズオプションがありますか?
ここでは、現時点での私の関連する方法、CollectionViewのすべての静的なサイズです:iOSの初心者を助けるため
override func viewDidLoad() {
super.viewDidLoad()
ExitNameLabel.text = exitName
let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 120 , height: 120)
self.imagesCollectionView.setCollectionViewLayout(layout, animated: true)
exitLocationDetails.text = exitLocation
imagesCollectionView.delegate = self
imagesCollectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return imageUrls.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:ImageCollectionViewCell = imagesCollectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
if (images_cache[imageUrls[indexPath.row]] != nil) {
cell.imageCell.image = images_cache[imageUrls[indexPath.row]]
} else {
loadImage(imageUrls[indexPath.row], imageview:cell.imageCell)
}
return cell
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let screenSize: CGRect = UIScreen.main.bounds
let width = screenSize.width
return CGSize(width: CGFloat(width), height: imagesCollectionView.bounds.size.height)
}
感謝:)
ああを試してみてください!私はそれを考え出した。これは、イメージがすでにキャッシュまたはダウンロードされている場合に機能します。私が必要としたのは、イメージのロードが完了した後に 'self.imagesCollectionView.reloadData()'を呼び出すことでした。ありがとう! – jeanmw