Created
May 3, 2017 19:46
-
-
Save catqbat/acbdf2149a2645142e840ebabd501e42 to your computer and use it in GitHub Desktop.
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 LocationDataSource: NSObject, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout | |
{ | |
let locations: [LocationModel]; | |
let setSelectedLocation: SetLocationDelegate; | |
init(locations: [LocationModel], delegate: @escaping SetLocationDelegate) | |
{ | |
self.locations = locations; | |
self.setSelectedLocation = delegate; | |
} | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int | |
{ | |
return locations.count; | |
} | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell | |
{ | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LocationCell", for: indexPath) as! LocationCell; | |
let location = locations[indexPath.row]; | |
cell.locationName = location.name; | |
return cell; | |
} | |
func collectionView(_ collectionView: UICollectionView, | |
layout collectionViewLayout: UICollectionViewLayout, | |
sizeForItemAt indexPath: IndexPath) -> CGSize | |
{ | |
let width = locations[indexPath.row].name.width(withConstrainedHeight: 50, font: LocationCell.locationNameFont); | |
return CGSize(width: width + 10, height: 50); | |
} | |
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) | |
{ | |
let location = locations[indexPath.row]; | |
setSelectedLocation(location); | |
} | |
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment