【问题标题】:Swift UICollectionView Cells Mysteriously RepeatSwift UICollectionView 单元格神秘地重复
【发布时间】:2016-06-23 20:47:14
【问题描述】:

我们正在尝试创建一个集合视图。在每个单元格中,用户可以选择图像并将文本输入文本字段。我们注意到,在添加四个单元格后,当我们添加一个新单元格时,文本字段已经填充了之前单元格的信息。在我们的代码中,我们从不以编程方式填充文本字段(一开始是空的),我们允许用户这样做。有什么建议吗?

  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
}

【问题讨论】:

    标签: ios xcode swift collectionview


    【解决方案1】:

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

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

    【讨论】:

    • 这正是这个方法的用途。 :)
    • @Abizern prepareForReuse from Apple Doc:如果单元格是可重用的(具有重用标识符),则在从表视图方法 dequeueReusableCellWithIdentifier: 返回单元格之前调用它。如果你覆盖,你必须调用 super。
    • @GentBerani 我知道。我确认这是应该使用的方法。
    【解决方案2】:

    问题是您正在使用dequeReusableCellWithIdentifier,它返回已创建的单元格(您之前使用过的单元格)。这就是为什么它已经充满了以前的数据。您需要在显示此单元格之前清除此数据,或者从某些存储中填充它(例如,代表您的集合视图单元格的数组(数组中的每个对象都与单元格相关,在您的情况下是写入单元格中的文本))

    【讨论】:

    • 你知道为什么数据在 4 个单元格后重复吗?因此,第 4 个单元格将包含与第一个单元格相同的数据,第五个单元格将包含与第 2 个单元格相同的数据,依此类推。
    • 是的,因为你的屏幕尺寸可以一次查看4个单元格,这就是为什么它出现在4个单元格之后。
    • 如果你需要的话,我明天可以帮你解决这个问题或者深入描述一下。
    • 我刚刚想通了!谢谢!
    • @OliviaWatkins 你是怎么解决的?我可能有类似的问题。
    【解决方案3】:

    这就是我最终解决它的方法。

    我创建了一个Item 类,其中包含集合视图单元格中显示的所有字段,并创建了一个项目数组。

    这是我的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!
        }
    }
    

    这是我的视图控制器类中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
    }
    

    【讨论】:

      【解决方案4】:

      原因是collectionViewLayout.collectionViewContentSize.height

      高于实际内容大小!建议保持 UICollectionView 自动计算高度(不使用 UIScrollView,让 UICollectionView 保持滚动),因为手动更改会导致很多奇怪的行为。

      【讨论】:

        猜你喜欢
        • 2014-11-04
        • 1970-01-01
        • 1970-01-01
        • 2017-05-03
        • 1970-01-01
        • 1970-01-01
        • 2014-07-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多