Skip to content

Instantly share code, notes, and snippets.

@catqbat
Created May 3, 2017 19:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save catqbat/acbdf2149a2645142e840ebabd501e42 to your computer and use it in GitHub Desktop.
Save catqbat/acbdf2149a2645142e840ebabd501e42 to your computer and use it in GitHub Desktop.
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