2017-07-03 100 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

为什么会出现这种情况?

更新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数组,然后重新加载collectionView。

如果问题是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:

相关问题