我正在尝试构建像Pinterest一样的集合视图布局。目标C中有大部分内容,所以我使用了这个RW教程:http://www.raywenderlich.com/107439/uicollectionview-custom-layout-tutorial-pinterest
问题是RW教程中的应用程序使用本地图像,而我正在尝试将单元格大小基于通过PinRemoteImage下载的图像,但是一旦图像出现,我就无法使collectionView正确地再次出现。下载。
以下是我尝试修改扩展名:
extension PinCollectionViewController : PinterestLayoutDelegate {
// 1
func collectionView(collectionView:UICollectionView, heightForPhotoAtIndexPath indexPath: NSIndexPath,
withWidth width: CGFloat) -> CGFloat {
var pinterestLargestImage = UIImage()
if imageDownloads == 0 {
pinterestLargestImage = imageArray[indexPath.row]
} else {
pinterestLargestImage = UIImage(named: "testPic")!
}
let boundingRect = CGRect(x: 0, y: 0, width: width, height: CGFloat(MAXFLOAT))
let rect = AVMakeRectWithAspectRatioInsideRect(pinterestLargestImage.size, boundingRect)
return rect.size.height
}
// 2
func collectionView(collectionView: UICollectionView,
heightForAnnotationAtIndexPath indexPath: NSIndexPath, withWidth width: CGFloat) -> CGFloat {
var pinterestLargestImage = UIImage()
if pins.count == imageArray.count {
pinterestLargestImage = imageArray[indexPath.row]
} else { pinterestLargestImage = UIImage(named: "testPic")!}
let annotationPadding = CGFloat(4)
let annotationHeaderHeight = CGFloat(17)
let font = UIFont(name: "AvenirNext-Regular", size: 10)!
let commentHeight = CGFloat(10.0)
let height = annotationPadding + annotationHeaderHeight + commentHeight + annotationPadding
return height
}
}
然后,我尝试在下载单元格图像后在self.collectionView(self.collectionView!, layout: (self.collectionView?.collectionViewLayout)!, sizeForItemAtIndexPath: indexPath)
内调用self.collectionView!.reloadItemsAtIndexPaths([indexPath])
和cellForRowAtIndexPath
,但都没有正确调用这些方法来调整布局。任何人都能指出我在正确的方向吗?