Resistant Resistant - 5 months ago 12
Swift Question

UICollectionView in Swift code? Why won't my code work?

I'm trying to make a UICollectionView in Swift using code only. I have deleted the Main.story and done all then setup. It runs fine but the cells never display. I followed a tutorial on YouTube and I swear my code is exactly the same(except for variable names or whatever) but it won't ever show the cells. If anyone can figure out why, I would greatly appreciate it. This is the ViewController file and at the bottom I added the AppDelegate file. I can change the background of the entire controller, but changing cell background color does nothing.

import UIKit

class CustomCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

let customCellIdentifier = "customCellIdentifier"

override func viewDidLoad() {
super.viewDidLoad()

collectionView?.backgroundColor = .greenColor()

collectionView?.registerClass(CustomCell.self, forCellWithReuseIdentifier: customCellIdentifier)
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let customCell = collectionView.dequeueReusableCellWithReuseIdentifier(customCellIdentifier, forIndexPath: indexPath)
return customCell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(view.frame.width, 100)
}


}

class CustomCell: UICollectionViewCell {

override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}

let nameLabel: UILabel = {
let label = UILabel()
label.text = "Custom Text"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

func setupViews() {
backgroundColor = UIColor.redColor()
addSubview(nameLabel)
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}


import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.makeKeyAndVisible()

let flowLayout = UICollectionViewLayout()
let customCollectionViewController = CustomCollectionViewController(collectionViewLayout: flowLayout)

window?.rootViewController = UINavigationController(rootViewController: customCollectionViewController)

// Override point for customization after application launch.
return true
}

Answer

Taken from iOS documentation here:

The UICollectionViewLayout class is an abstract base class that you subclass and use to generate layout information for a collection view. The job of a layout object is to determine the placement of cells, supplementary views, and decoration views inside the collection view’s bounds and to report that information to the collection view when asked. The collection view then applies the provided layout information to the corresponding views so that they can be presented onscreen.

You must subclass UICollectionViewLayout in order to use it. Before you consider subclassing, though, you should look at the UICollectionViewFlowLayout class to see if it can be adapted to your layout needs.

So you probably should change

let flowLayout = UICollectionViewLayout()

with

let flowLayout = UICollectionViewFlowLayout()
Comments