Displaying data in a collection view

You've implemented a data manager class, ExploreDataManager, that reads cuisine data from a .plist file and stores it in an array of ExploreItem instances. Now, you will use that array as the data source for the collection view in the Explore screen.

At present, the collection view in the Explore screen displays 20 collection view cells, with each cell containing an empty image view and a label. You need a way to set the values for the image view and the label in the cells, so you will create a view controller, ExploreCell, for this purpose. Then, you can configure the view controller for the collection view, ExploreViewController, to get cuisine details from ExploreDataManager and provide it to the collection view for display.

To create ExploreCell, perform the following steps:

  1. Right-click on the Explore folder in the Project navigator and choose New Group.
  2. Rename the new group View.
  3. Right-click on the View folder and select New File.
  4. iOS should already be selected. Choose Cocoa Touch Class, then click Next.
  1. Configure the class as shown here:
  • Class: ExploreCell
  • Subclass: UICollectionViewCell
  • Also create XIB: Unchecked
  • Language: Swift
  • Click Next
  1. Click Create.
  2. A new file, ExploreCell, will be added to your project. Click it and you will see the following:
import UIKit
class ExploreCell: UICollectionViewCell {
}
  1. Open Main.storyboard and click exploreCell inside the Explore View Controller Scene in the document outline. Click the Identity inspector:

  1. Set the Custom Class to ExploreCell. This sets ExploreCell as the view controller for exploreCell. Press Return when done:

Now, you'll create outlets in ExploreCell that will be connected to the image view and the label in exploreCell, so you can control what exploreCell displays.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.77.208