【问题标题】:Swift - UICollectionView removeAll() temporarily clears the cellsSwift - UICollectionView removeAll() 临时清除单元格
【发布时间】:2017-07-03 05:05:09
【问题描述】:

我正在向 UICollectionView 动态添加数据。每当我有新数据时,它都会清除所有现有数据并加载一个新数据集。这是代码,

self.conversation.removeAll()
self.collectionView.reloadData()

self.conversation.insert(messageWrapper(description: "Some text", origin: "user"), at: 0)
self.collectionView.reloadData()

ItemAtIndexPath中的代码

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        let textViewCell = cell as! TextCollectionViewCell
        let description = conversation[indexPath.row].description
        let origin = conversation[indexPath.row].origin

        textViewCell.textView.text = description
        textViewCell.textView.textAlignment = origin == "arvis" ? NSTextAlignment.left : NSTextAlignment.right
    }

问题它删除了所有现有数据,在加载新数据时,它与以前的数据重叠,即假设我有Hello,而在添加I am good时,它显示@987654328 @ 和“我很好”。

为什么会这样?

更新 1:
cellForItemAt

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let textViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "textViewCell", for: indexPath) as! TextCollectionViewCell
        let description = conversation[indexPath.row].description
        let origin = conversation[indexPath.row].origin

        textViewCell.awakeFromNib()
        textViewCell.textView.text = description
        textViewCell.textView.textAlignment = origin == "arvis" ? NSTextAlignment.left : NSTextAlignment.right

        return textViewCell
    }

TextCollectionViewCell
单元格的标识符类

class TextCollectionViewCell: UICollectionViewCell {

    var textView: UITextView!

    override func awakeFromNib() {
        textView = UITextView(frame: contentView.frame)
        textView.textColor = UIColor.white
        textView.font = UIFont.systemFont(ofSize: 18)
        textView.layer.backgroundColor = UIColor.clear.cgColor
        textView.contentMode = .scaleAspectFill
        textView.clipsToBounds = true
        contentView.addSubview(textView)
    }
}

【问题讨论】:

    标签: ios iphone swift uicollectionview uicollectionviewcell


    【解决方案1】:

    现有数据被清除,因为您正在清除 collectionView 的数据源并重新加载它。

    只需尝试将新值附加到 conversation 数组,然后重新加载 collectionView。

    如果问题在于重叠,请尝试从cellForItemAtIndexPath 更新textViewCell.textView,而不是在willDisplaycell 中更新textViewCell.textView

    【讨论】:

    • 是的,没错!我想清除所有现有数据,效果很好!但问题是它在将新数据加载到对话时再次出现。
    • 能否请您发布 cellForItemAtIndexPath 的代码
    • @aravindtrue 而不是在willDisplaycell 中更新textViewCell.textView 尝试从cellForItemAtIndexPath 更新textViewCell.textView 并检查
    • @AravindAR,看起来,它不起作用!我在UPDATE 1 部分添加了我的代码
    • @aravindtrue 尝试在 textViewCell.textView.text = description 上面添加 textViewCell.textView.text = nil 看看它是否有效?
    【解决方案2】:
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        let textViewCell = cell as! TextCollectionViewCell
    
        let description = conversation[indexPath.row].description
            let origin = conversation[indexPath.row].origin
    
            textViewCell.textView.text = description
            textViewCell.textView.textAlignment = origin == "arvis" ? NSTextAlignment.left : NSTextAlignment.right
    
        return textViewCell
    }
    

    试试这个而不是更新willDisplay cell:使用cellForItemAt indexPath:

    【讨论】:

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