C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: Scroll down to Collection View Controller. Click and drag it to the part of the window where the View Controller is listed.
ExampleCollectionController file: Swift
//
// ExampleCollectionController.swift
// ExampleTime4
//
// ....
//
import Foundation
Here: We specify how our Collection View gets its data. The names of the 3 funcs are important—copy and paste them.
Note: The first two funcs return Ints. These indicate to the Collection View how many elements are in the current section.
Finally: The third func (named collectionView) returns an instance of UICollectionViewCell. It is used to render all cells.
ExampleCollectionController, three funcs: Swift
//
// ExampleCollectionController.swift
// ExampleTime4
//
// ....
//
import UIKit
// This class should inherit from UICollectionViewController.
class ExampleCollectionController: UICollectionViewController {
override func numberOfSectionsInCollectionView(
collectionView: UICollectionView) -> Int {
// Use 3 for demonstration.
return 3
}
override func collectionView(collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
// Use 3 for demonstration.
return 3
}
override func collectionView(collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// Get reusable cell.
// ... Use identifier "Example."
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Example",
forIndexPath: indexPath)
// Make the cell blue.
cell.backgroundColor = UIColor.blueColor()
// Return the cell.
return cell
}
}
Warning: The above code implementation is not finished: it returns the Int 3, and does nothing useful.
Quote: You create a custom subclass of UICollectionViewController for each collection view that you want to manage.
UICollectionView Controller: apple.comSo: Please locate your Collection View Controller in Xcode again. Find the cell in the list.
Tip: We must provide a reusable identifier on a cell. Type "Example" into Xcode to specify this (it can be anything, but must match).
Blue: The blue boxes are the cells. Please see how the UIColor.blueColor() method is called in Swift.
Aqua: The background on the UICollectionView is aqua. It covers the entire screen which is not usually ideal.
Animals: We add a 3-element string array. We use "animals.count" in the collectionView function for the item count.
UILabel: In the third func, we some code to create a UILabel and add it to the contentView with addSubview.
Result: Our UICollectionView now has labels based on the contents of a string array. We have blue and white colors.
ExampleCollectionController, string array: Swift
//
// ExampleCollectionController.swift
// ExampleTime4
//
// ....
//
import UIKit
class ExampleCollectionController: UICollectionViewController {
// Use a 3-item string array.
let animals = ["bird", "dog", "cat"]
override func numberOfSectionsInCollectionView(
collectionView: UICollectionView) -> Int {
// Just 1 section for now.
return 1
}
override func collectionView(collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
// Item count equals string array length.
return animals.count
}
override func collectionView(collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// Get reusable cell.
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Example",
forIndexPath: indexPath)
// Make the cell blue.
cell.backgroundColor = UIColor.blueColor()
// Create a UILabel and index the array based on the indexPath.
// ... Size the Label.
let label = UILabel()
label.text = animals[indexPath.item]
label.textColor = UIColor.whiteColor()
label.sizeToFit()
// Add label to the contentView.
cell.contentView.addSubview(label)
// Done.
return cell
}
}