In this UICollectionView tutorial, you'll power up your collection view skills by learning about cell selection, cell reordering, and supplementary views.
Nice tutorial, thanks. A very welcome follow-up would be a tutorial on how to hook these up to an NSFetchedResultsController. No one ever seems to cover this aspect properly. As great a tool as they are for data display, not explaining how to connect them to iOSโs native persistent data store always seems a bit odd.
override func collectionView(_ collectionView: UICollectionView,
moveItemAtIndexPath sourceIndexPath: IndexPath,
to destinationIndexPath: IndexPath) {
var sourceResults = searches[(sourceIndexPath as NSIndexPath).section].searchResults
let flickrPhoto = sourceResults.remove(at: (sourceIndexPath as NSIndexPath).row)
var destinationResults = searches[(destinationIndexPath as NSIndexPath).section].searchResults
destinationResults.insert(flickrPhoto, at: (destinationIndexPath as NSIndexPath).row)
}
would have no effect on the actual seaches array. This is all code that lives within the function but makes no difference to anything. It should be changed to something like this:
override func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let sourceSection = searches[sourceIndexPath.section]
var newSearchResults = sourceSection.searchResults
let movedPhoto = newSearchResults.remove(at: sourceIndexPath.row)
searches[sourceIndexPath.section] = FlickrSearchResults(searchTerm: sourceSection.searchTerm, searchResults: newSearchResults)
let destinationSection = searches[destinationIndexPath.section]
newSearchResults = destinationSection.searchResults
newSearchResults.insert(movedPhoto, at: destinationIndexPath.row)
searches[destinationIndexPath.section] = FlickrSearchResults(searchTerm: destinationSection.searchTerm, searchResults: newSearchResults)
}
Thanks for the great tutorial. How would you implement a long press detection on a cell?
The following checks for the selection. What if you wanted to check for a long press ?
Thank you
Excellent tutorial and very helpful! Thank you. I converted it to Swift 4 and IOS 10. And thanks for the correction to moveItemAt. For example, the share bar button action could be:
@IBAction func share(_ sender: UIBarButtonItem) {
// We have no photos
guard !searches.isEmpty
else { return }
// We have not selected photos
guard !selectedPhotos.isEmpty
else { sharing = !sharing; return }
if sharing {
let imageArray: [UIImage] = selectedPhotos.map { return $0.thumbnail! }
if !imageArray.isEmpty {
let shareScreen = UIActivityViewController(activityItems: imageArray, applicationActivities: nil)
shareScreen.completionWithItemsHandler = {
[unowned self] (activityType, completed, returnedItems, activityError) in
self.sharing = false
}
let popoverPresentationController = shareScreen.popoverPresentationController
popoverPresentationController?.barButtonItem = sender
popoverPresentationController?.permittedArrowDirections = .any
present(shareScreen, animated: true, completion: nil)
}
}
}
This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]