Swift UICollectionView单元格神秘地重复

Oli*_*ins 2 xcode collectionview ios swift

我们正在尝试收集视图。用户可以在每个单元格中选择一个图像,然后在文本字段中输入文本。我们注意到,在添加四个单元格之后,当我们添加一个新的单元格时,文本字段已经充满了先前单元格中的信息。在我们的代码中,我们永远不会以编程方式填充文本字段(该字段以空开头),我们允许用户执行此操作。有什么建议么?

  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Image", forIndexPath: indexPath) as! CollectionViewCell
    cell.deleteButton?.addTarget(self, action: #selector(AddNewItem.xButtonPressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
    cell.deleteButton?.layer.setValue(indexPath.row, forKey: "index")
    let item = items[indexPath.item]
    let path = getDocumentsDirectory().stringByAppendingPathComponent(item.image)
    cell.imageView.image = UIImage(contentsOfFile: path)
    cell.imageView.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3).CGColor
    cell.imageView.layer.borderWidth = 2
    cell.layer.cornerRadius = 7

    return cell
}
Run Code Online (Sandbox Code Playgroud)

小智 5

您可以在UICollectionViewCell自定义类中使用它

override func prepareForReuse() {
    self.profileImg.image = #imageLiteral(resourceName: "Profile Icon Empty")

    super.prepareForReuse()
}
Run Code Online (Sandbox Code Playgroud)