Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derrickshowers/b98c3a7b092f7dc70decf2cf042f2407 to your computer and use it in GitHub Desktop.
Save derrickshowers/b98c3a7b092f7dc70decf2cf042f2407 to your computer and use it in GitHub Desktop.
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