0
我正在将联系人导入到我的应用程序中并将它们插入到tableview中。如何停止TableViewCell重复图像
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: importCellId, for: indexPath) as! tbcImportContact
cell.contact = contacts[indexPath.row]
return cell
}
当我设置联系人图像像我在这里做什么:
var contact: Contact? {
didSet {
if let name = contact?.name {
nameLabel.text = name
} else {
nameLabel.text = ""
}
if let img = contact?.contactImage {
contactImage.image = img
} else {
contactImage.image = UIImage(named: "emptyProfile")
}
}
与nil
图像的下一次接触应该得到UIImage(named: "emptyProfile")
而是接触分配之前的接触图像。
我已经尝试添加else
语句来防止这种情况,但它不起作用。 我也尝试添加:
override func prepareForReuse() {
self.contactImage.image = UIImage(named: "emptyProfile")
self.nameLabel.text = ""
}
分配接触到它时,重置细胞,但也没有工作。 我也尝试在主线程上将其设置为异步,并且也没有工作。
我还有什么建议可以尝试解决这个问题?
我把在'if let img ...'这一行上有一个断点,看看它在为不同情况做了什么。 (例如,联系人是否是我期望的,以及对于有效或空的图像属性发生了什么) –
@PhillipMills联系人图像进入零后,直到设置了图像的第一个联系人,然后如果联系人图像是零,它从之前的联系中带回来的图像不是零 – RickS
这是什么代码? '如果让img'评估为真? –