You'll often want to respond in some way when a collection view cell is selected. Maybe you'll zoom in on a photo or present some other type of data to your user. See how to respond to cell selections in this video.
- [Instructor] So let's say we want to transition…to another view controller when an item…in the collection view is selected.…To do that, we can actually handle the method,…collectionView didSelectItemAt indexPath.…So type out collectionView,…find collectionView, didSelectItemAt indexPath.…If you see an error like this, just click on it.…It'll say that you have to add the override keyword.…So just hit fix, and there you go,…the override keyword is added.…
So inside of here, we need to write the code…to handle selecting an item.…Let's say we want to store the index that's selected.…So we grab indexPath.row, so we could send…the appropriate item into the view controller…that we transition to later on.…So I'll go up to the top of my code.…I'll create a variable called currentIndex,…initialize it to zero.…Now I'll scroll down, and inside of the code,…type currentIndex equals indexPath.row.…
Now what if we wanted to segue to the other view?…Well, we could do that by heading over…to our storyboard, and creating a segue.…
- Alert controllers and actions
- Sliders, progress bars, and switches
- Creating UI elements with code
- Displaying web content with Safari view controllers and WKWebView
- Creating collection view controllers
- Creating tabbed apps
- Publishing your app with App Store Connect
Skill Level Beginner
1. Master Views
2. Display Web Content
3. Collection Views
4. Multiview Templates
Customize a table view cell4m 18s
5. Publish an App
Next steps3m 28s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.