Last active
October 21, 2018 14:40
-
-
Save Oni-zerone/70e669508daf99596a245bdf9950e853 to your computer and use it in GitHub Desktop.
An example of what you could find in a UICollectionViewDataSource
This file contains 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 StandardCell: UICollectionViewCell { | |
var titleLabel: UILabel! | |
} | |
class StandardDataSource: NSObject, UICollectionViewDataSource { | |
var firstSectionItems: [String] = ["first", "second", "third"] | |
var secondSectionItems: [String] = ["FIRST", "SECOND", "THIRD"] | |
func numberOfSections(in collectionView: UICollectionView) -> Int { | |
return 2 | |
} | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
if section == 0 { | |
return firstSectionItems.count | |
} else if section == 1 { | |
return secondSectionItems.count | |
} else { | |
return 0 | |
} | |
} | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) | |
guard let standardCell = cell as? StandardCell else { | |
return cell | |
} | |
if indexPath.section == 0 { | |
standardCell.titleLabel.text = self.firstSectionItems[indexPath.item] | |
} else if indexPath.section == 1 { | |
standardCell.titleLabel.text = self.firstSectionItems[indexPath.item] | |
} | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment