ios - 单元格突出显示默认图像而不是实际图像
问题描述
我在表格的单元格中有头像。当我触摸一个单元格(突出显示)时,它会显示默认头像图像而不是实际图像。我不确定是什么原因造成的以及如何解决。有什么帮助吗?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as? TableViewCell else {
fatalError("Can't find cell")
}
//...
cell.selectionStyle = .default
self.configCell(cell: cell, indexPath: indexPath)
//...
}
func configCell(cell: TableViewCell, indexPath: IndexPath) {
//...
// Avatar
if let url = URL(string: urlString) {
cell.avatarImageView.image = .defaultImage
cell.tag = indexPath.row
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else { return }
DispatchQueue.main.async {
if cell.tag == indexPath.row {
cell.avatarImageView.image = UIImage(data: data)
}
}
}
task.resume()
}
//...
}
解决方案
如果您仅在正确显示图像时遇到问题,我建议您highlightedImage
使用UIImageView
.
UITableViewCell
具有.highlighted
属性(当单元格被按下时)。因此,如果单元格包含UIImageView
内部,那么当您选择/突出显示单元格时,UIImageView
将使用.highlightedImage
代替 just .image
。
因此,作为对问题的备份和修复,您还可以提供/告诉UIImageView
显示头像,即使它被突出显示。
推荐阅读
- java - Java Spring Security OAuth2:通过 POST 接受客户端凭据
- javascript - 订阅 - 带有 AWS AppSync 的 Vue-Apollo - 未处理的 GraphQL 订阅错误
- java - CLOSED 将一个单词添加到单词列表中
- javascript - JavaScript:匹配不是函数
- java - 如何在 Spring Boot 应用程序中初始化 AWS SDK?
- java - 永远隐藏导航栏
- ios - iOS 10,swift 4.1,将视图的顶部锚点设置为 topLayoutGuide 底部锚点导致冲突
- asp.net-mvc - 同时添加数百万条记录
- r - R:对特定子集按行分组和求和
- angular - ngrx router-store - 测试路由器导航效果