2017-07-03 12 views
0

私は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と表示しています。

enter image description here

これはなぜ起こるのでしょうか?

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) 
    } 
} 

答えて

0

あなたがのデータソースをクリアしているので、既存のデータがクリアされますcollectionViewを呼び出して再読み込みします。

新しい値をconversation配列に追加してから、コレクションビューを再ロードしてみてください。

問題がoveralappingである場合はちょうど、cellForItemAtIndexPathからtextViewCell.textViewを更新する代わりにwillDisplaycell

+0

はいにアップデートしてみてください!私はすべての既存のデータをクリアしたい、うまくいく!しかし、問題は、新しいデータを会話にロードしている間に再度表示されることです。 – moustacheman

+0

cellForItemAtIndexPathのコードを投稿してください。 –

+0

@aravindtrueを 'willDisplaycell'で' textViewCell.textView'を更新するのではなく、cellForItemAtIndexPathから 'textViewCell.textView'を更新して、 –

0
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 
} 

textViewCell.textViewを更新し、これを試す代わりにwillDisplay cell:使用cellForItemAt indexPath:

関連する問題