我正在尝试让我的标签和图像动态地改变uicollectionviewcell的高度。如何根据获得的每张图像估算出细胞的正确高度?它期望宽度和高度,但我不想扭曲图像。
我正在关注这个cocoapod:https://github.com/ecerney/CollectionViewWaterfallLayout
或者是否有更好的方法来设置标签/文字?
目前标签和图像之间的间距未设置...
故事板内的单元格如下所示,UIImageView设置为Aspect Fill:
获取图像集的代码
lazy var imageSet: [UIImage] = {
var _imageSet = [UIImage]()
for x in 0...10{
let array = [600,800,900]
let array2 = [1000,1200,1400]
let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
let randomIndex2 = Int(arc4random_uniform(UInt32(array2.count)))
let urlString:String = String(format: "http://lorempixel.com/%@/%@/", String(array[randomIndex]),String(array2[randomIndex2]))
let image = UIImage(data: NSData(contentsOfURL: NSURL(string: urlString)!)!)
print(urlString)
print("\(x)\n")
_imageSet.append(image!)
}
return _imageSet
}()
设置每个单元格大小的代码......
func collectionView(collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let thisLayout = layout as! CollectionViewWaterfallLayout
var globalImage = imageSet[indexPath.row]
var finalWith = globalImage.size.width
var finalHeight = globalImage.size.height
var newSize = CGSize(width: finalWith, height: finalHeight )
return newSize
}