Swift可重复使用的细胞图像被重用

3 deque uitableview ios swift

我试图只在某些条件下才能在我的UITableView的某些单元格中显示图像.满足条件时,图像将按预期显示,但是当我向下滚动列表时,即使条件未满足,也会在单元格中向下看到图标.我认为这对于将单元格作为原型并在代码中重用而具有某些功能,但无法弄清楚如何修复.这是我的代码:

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
}
Run Code Online (Sandbox Code Playgroud)

Cal*_*leb 6

在else中添加:

cell.imageView?.image = nil
Run Code Online (Sandbox Code Playgroud)

说明:UITableViews重用单元格.因此,您必须将属性设置为两种方案的帐户.