简体   繁体   English

Swift UICollectionView 单元格神秘地重复

[英]Swift UICollectionView Cells Mysteriously Repeat

We are trying to make a collection view.我们正在尝试创建一个集合视图。 In each cell the users can choose an image and enter text into a text field.在每个单元格中,用户可以选择图像并在文本字段中输入文本。 We noticed that after adding four cells, when we add a new cell, the text field is already filled with the information from previous cells.我们注意到添加四个单元格后,当我们添加一个新单元格时,文本字段已经填充了来自先前单元格的信息。 In our code, we never programmatically fill the text field (which starts out empty), we allow the user to do this.在我们的代码中,我们从不以编程方式填充文本字段(开始时为空),我们允许用户这样做。 Any suggestions?有什么建议吗?

  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
}

You can use this in UICollectionViewCell custom class您可以在 UICollectionViewCell 自定义类中使用它

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

    super.prepareForReuse()
}

Problem is that you are using dequeReusableCellWithIdentifier which returns already created cell(that you were using before).问题是您正在使用dequeReusableCellWithIdentifier它返回已创建的单元格(您之前使用过)。 That's why it's already filled with previous data.这就是为什么它已经填充了以前的数据。 You need to clear this data before showing this cell, or fill it from some storage(for example array that represents your collection view cells(each object in array somehow related to cell, in your case that is text wroten in cell))您需要在显示此单元格之前清除此数据,或从某些存储中填充它(例如代表您的集合视图单元格的数组(数组中的每个对象都与单元格有某种关系,在您的情况下是在单元格中写入的文本))

Here's how I ultimately ended up resolving it.这就是我最终解决它的方式。

I created an Item class which contained all of the fields which are shown in the collection view cell and created an array of Items.我创建了一个Item类,其中包含集合视图单元格中显示的所有字段,并创建了一个 Items 数组。

Here is a simplified version of my CollectionViewCell class, which here only has a single text field:这是我的CollectionViewCell类的简化版本,这里只有一个文本字段:

class CollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var itemName: UITextField!

    var item: Item?

    func initializeListeners(){
        itemName.addTarget(self, action: #selector(itemNameChanged(_:)), forControlEvents: UIControlEvents.EditingChanged)
    }


    //When the item name is changed, make sure the item's info is updated
    func itemNameChanged(textField: UITextField) {
        item?.itemName = textField.text!
    }
}

Here's a simplified version of the cellForItemAtIndexPath function in my view controller class:这是我的视图控制器类中cellForItemAtIndexPath函数的简化版本:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell
    cell.initializeListeners()
    let item = items[indexPath.item]
    cell.item = item
    cell.itemName.text = item.itemName
    return cell
}

The reason is that collectionViewLayout.collectionViewContentSize.height原因是collectionViewLayout.collectionViewContentSize.height

is taller than the real contents size!比实际内容尺寸高! It is recommended to keep UICollectionView calculate the height automatically (without using UIScrollView, let UICollectionView maintain the scroll), as manual change will cause lots of weird behaviors.建议保持 UICollectionView 自动计算高度(不使用 UIScrollView,让 UICollectionView 保持滚动),因为手动更改会导致很多奇怪的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM