【发布时间】:2018-03-28 08:12:41
【问题描述】:
我有一个聊天记录,它只是一个UICollectionView。每个销售都有一个头像 (UIImage) 和一个文本气泡。我正在尝试通过使用 Alamofire 从服务器获取头像 URL 来用正确的图像填充头像,如下所示:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "chatMessage", for: indexPath) as! ChatMessageCell
let imageURL = URL(string: messagesArray![indexPath.item].userAvatar)
Alamofire.download(imageURL!).responseData { response in
if let data = response.result.value {
cell.userAvatar.image = UIImage(data: data)
}
}
cell.messageText.text = messagesArray![indexPath.item].userText
}
我的问题:在某些单元格中头像出现在某些不是。我认为这与 Alamofire 异步工作有关。所以我的问题是如何正确地将图像填充到UICollectionView 以显示每个单元格中的每个头像?
【问题讨论】:
-
你觉得呢?你做了任何调试吗?您是否确认所有调用都成功,您正在取回数据并为它们创建图像?
-
您应该考虑创建一个带有活动指示器的视图。在进行网络呼叫之前,请调出此视图并激活活动指示器。这是标准做法。当网络调用完成时,停止指示器,关闭视图,然后重新加载该点的数据。
标签: ios swift alamofire uicollectionviewcell alamofireimage