、あなたできreloadData()
とそれは働くだろう! --- 非常に非効率的です :(コレクションビューの1000個のアイテムが1001番目のアイテムを追加するためにロードされていると想像してください)と1つのアイテムを更新したいだけです。よりよいなぜ、私たちは、インサート機能を持っており、それを使用する
- >あなたはこの後に添付のような何かを試すことができます。
let count = yourStringArray.count // updated Count of array
let index = count > 0 ? count - 1 : count // find the index, where you want to add the item.
let indexPath = NSIndexPath(forItem: index, inSection: 0) // make an indexPath out of the found index!
collectionView!.insertItemsAtIndexPaths([indexPath]) // This will call dataSource methods itself
// And you should be good to go!
:-)もし何か疑問、質問をします
編集:コードが別のクラスのコメントで提案を上キャリング
からコレクションビューを更新するために、あなたはviewDidLoad
にとても好きcollectionViewクラスにオブザーバーを追加することができます。
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourCollectionViewController.loadList(_:)),name:"load", object: nil)
// And add this function in class
func loadList(notification: NSNotification){
let count = yourStringArray.count
let index = count > 0 ? count - 1 : count
let indexPath = NSIndexPath(forItem: index, inSection: 0)
collectionView!.insertItemsAtIndexPaths([indexPath])
}
を
他のクラスでこれを行うことができます:
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
追加: 通知でオブジェクトを送信できることに注意してください。オブジェクトを送信して、コレクションビューのクラス自体に配列に追加することができます
そうですか? :
NSNotificationCenter.defaultCenter().postNotificationName("load", object: someDateasStringType)
そしてあなたは、この日付を受け取り、そのように非常にクラス(あなたのcollectionviewクラス)に追加することができます
func loadList(notification: NSNotification){
let returnedDate = notification.object as! String
// convert string to Date. and append and insert to collection View (all mentioned above.)
}
新しい文字列を配列 'collview.reloadData()に追加するときこれを追加します –
コレクションビューを再ロードする必要がありますデータが変更された後:collectionViewVariable.reloadData() – Sam
@BhavinRamani、これは迅速な質問です。迅速な回答で返信してください – Sam