Created
March 19, 2017 22:20
-
-
Save derrickshowers/b98c3a7b092f7dc70decf2cf042f2407 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { | |
@IBOutlet weak var collectionView: UICollectionView! | |
var text = [ | |
"Some text.", | |
"Some longer text text text text text text text text text text.", | |
"Lots text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text." | |
] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupCollectionView() | |
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5) { | |
self.text = [ | |
"a", | |
"b", | |
"c", | |
"d" | |
] | |
self.collectionView.reloadData() | |
// Only a, b, c rows are displayed unless reloadData() is called once more here | |
// self.collectionView.reloadData() | |
} | |
} | |
private func setupCollectionView() { | |
collectionView.delegate = self | |
collectionView.dataSource = self | |
collectionView.register(UINib(nibName: "GenericCell", bundle: nil), forCellWithReuseIdentifier: "GenericCell") | |
(collectionView.collectionViewLayout as? UICollectionViewFlowLayout)?.estimatedItemSize = CGSize(width: 100, height: 100) | |
} | |
// MARK: - UICollecitonViewDataSource | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return text.count | |
} | |
// MARK: - UICollectionViewDelegate | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GenericCell", for: indexPath) as? GenericCell else { | |
return UICollectionViewCell() | |
} | |
cell.configure(labelText: text[indexPath.row]) | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment