Skip to content

Instantly share code, notes, and snippets.

@killev
Last active February 4, 2018 08:29
Show Gist options
  • Save killev/b802c213a1e1a9d05853ca7b869bcc28 to your computer and use it in GitHub Desktop.
Save killev/b802c213a1e1a9d05853ca7b869bcc28 to your computer and use it in GitHub Desktop.
UICollectionView changes problem
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array.count
}
var array : Array<Int> = [1,2,3]
override func viewDidLoad() {
super.viewDidLoad()
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.reloadData()
addItem(sender: nil)
addItem(sender: nil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
@IBAction func addItem(sender: AnyObject?){
array.append(10)
collectionView.insertItems(at: [IndexPath(row: 0, section: 0)])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment