我想从 Instagram API 获取图像,然后使用 SDWebImage
在我的 iOS 应用程序的 UICollectionView
中显示它们。但是在我将图像加载到单元格之后,单元格仍然是黑色的。
这是 cellForItemAtIndexPath:
中的代码:
let photo = self.photosArray[indexPath.row] as! AnyObject
let photoUrlString = photo.valueForKeyPath("images.standard_resolution.url")! as! String
print(photoUrlString) // properly show the URL
let imageView = UIImageView()
imageView.sd_setImageWithURL(NSURL(string: photoUrlString), placeholderImage: nil)
print(cell.frame) // confirmed that the frame is not 0
cell.contentView.addSubview(imageView)
但是,单元格在屏幕上保持黑色。
我还通过调用 cell.contentView.backgroundColor = UIColor.redColor()
检查它,然后它被包裹在红色屏幕中。但是图像仍然没有显示在屏幕上。
如何在单元格上显示图像?
更新
我尝试使用相同的 addSubview:
将 UILabel
添加到它的单元格中,标签显示正确。我不确定为什么图像不是这种情况...
最佳答案
您的 UIImageView 似乎没有框架。试试这个:
let imageView = UIImageView(frame: cell.contentView.frame.bounds)
关于ios - 与 SDWebImage 一起使用时,UICollectionViewCell 保持黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31643392/