私はUICollectionViewにデータを動的に追加しています。私は新しいデータを持っているときはいつも、既存のすべてのデータをクリアし、新しいセットを読み込みます。ここでは、コード、Swift - UICollectionView removeAll()はセルを一時的に消去します
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
を追加している間は、Hello
と表示しています。
これはなぜ起こるのでしょうか?
UPDATE 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)
}
}
はいにアップデートしてみてください!私はすべての既存のデータをクリアしたい、うまくいく!しかし、問題は、新しいデータを会話にロードしている間に再度表示されることです。 – moustacheman
cellForItemAtIndexPathのコードを投稿してください。 –
@aravindtrueを 'willDisplaycell'で' textViewCell.textView'を更新するのではなく、cellForItemAtIndexPathから 'textViewCell.textView'を更新して、 –