【发布时间】:2018-04-11 07:10:15
【问题描述】:
我正在使用以下自定义布局 https://www.raywenderlich.com/164608/uicollectionview-custom-layout-tutorial-pinterest-2 从集合视图中的 url 加载图像,并使用 kingfisher 将图像加载到集合视图单元格中,如下所示
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DbatCollectionViewCell",
for: indexPath) as! DbatCollectionViewCell
cell.layer.cornerRadius = 5
let debate = debatesArray[indexPath.row] as! NSDictionary
let debateDetails = debate.value(forKey: "debate") as! Debate
cell.dbatName?.text = debateDetails.title
if let debateImageUrl = URL(string: debateDetails.image!) {
cell.dbatImage.kf.setImage(with: debateImageUrl as Resource)
}
return cell
}
在该教程中,单元格的高度由图像高度提供,但我想根据图像的比例给出图像大小,无论是纵向还是横向图像。为此,我必须在
中找到图像的高度extension FavouriteCollectionViewController : DbatterLayoutDelegate {
// 1. Returns the photo height
func collectionView(_ collectionView: UICollectionView, heightForPhotoAtIndexPath indexPath:IndexPath) -> CGFloat {
let heights = [125,150, 175,200,225,250]
let number = heights[Int(arc4random_uniform(UInt32(heights.count)))]
return CGFloat(number)
}
}
请告知如何操作
【问题讨论】:
-
你解决了吗?
-
@Anushk 是的,我已经解决了。请参阅下面的答案
标签: swift uicollectionview uicollectionviewcell