gpt4 book ai didi

ios - Swift 可重用细胞图像被重用

转载 作者:搜寻专家 更新时间:2023-10-31 22:07:44 24 4
gpt4 key购买 nike

我试图让图像仅在特定条件下出现在我的 UITableView 的特定单元格中。满足条件时,图像会按预期显示,但当我向下滚动列表时,即使不满足条件,也会在底部的单元格中看到图标。我 gessing 这与单元格是原型(prototype)并在代码中重用有关,但无法弄清楚如何修复。这是我的代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("locCell", forIndexPath: indexPath)

var currentLoc = self.hereLocs[indexPath.row]
var hereImage = UIImage(named: "loc")

if currentLoc.currentlyHere! == true {
cell.textLabel?.text = currentLoc.name!
cell.detailTextLabel?.text = currentLoc.vicinity!
cell.imageView?.image = hereImage
} else {
cell.textLabel?.text = currentLoc.name!
cell.detailTextLabel?.text = currentLoc.vicinity!
}

return cell
}

最佳答案

在else中添加:

cell.imageView?.image = nil

解释:UITableViews 重用单元格。因此,您必须设置属性以考虑这两种情况。

关于ios - Swift 可重用细胞图像被重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31779343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com