ios - 与 SDWebImage 一起使用时,UICollectionViewCell 保持黑色

标签 ios swift uicollectionview sdwebimage

我想从 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/

相关文章:

swift - 在 ios Swift 中的 Images.xcassets 文件夹中的 Collection View 中显示图像

ios - 使用 iOS 确定设备(iPhone、iPod Touch)

ios - 如何检测短语?

ios5 - 商店工具包 - 获取不可续订订阅的到期日期

swift - ARSCNView 在 iOS 12 上因 GPU 错误 "Execution of the command buffer was aborted... (IOAF code 5)"而滞后

ios - 如何创建谓词来获取同一属性的所有不重复结果的实体?

ios - 如何调用嵌入了UICollectionView的UITableViewCell的indexPath.row

arrays - Swift 数组行为

ios - 为什么在 viewdidload 之前加载 Collection View 标题?

objective-c - iOS 7 : insertItemsAtIndexPaths: scrolls the UICollectionView