Search code examples
iosswiftuicollectionviewxibuicollectionviewcell

Use of undeclared type "tagCell" Error. Creating UICollectionViewCell that kind of xib


I am trying to build tag flow layout and I was following this topic;

http://codentrick.com/create-a-tag-flow-layout-with-uicollectionview/

Then this code threw this error : Use of undeclared type 'tagCell'.

var tagCell = collectionView.dequeueReusableCellWithReuseIdentifier("tagCell", forIndexPath: indexPath) as! tagCell

I created these files; a xib called tagCell.xib, a file called tagCell.swift. And I connected these files under xib's custom class. I don't know what is the problem and I followed the topic without missing. Maybe there was some changes about it in swift 2.

I need help. I looked for ready to use libraries written via swift but I didn't find. I have to code it myself for now.

There are the classes.

//
//  tagCell.swift
//  matchMyTag
//
//  Created by Faruk Turgut on 05/12/15.
//

import UIKit

class tagCell: UICollectionViewCell {

    @IBOutlet weak var tagTitle: UILabel!
    @IBOutlet weak var plusLabel: UILabel!

    override func awakeFromNib() {
        self.backgroundColor = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 1)
        self.tagTitle.textColor = UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 1)
        self.plusLabel.textColor = UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 1)
        self.layer.cornerRadius = 4

    }

}

//
//  skillsNeedsViewController.swift
//  matchMyTag
//
//  Created by Faruk Turgut on 05/12/15.
//  Copyright © 2015 Faruk Turgut. All rights reserved.
//

import UIKit

class skillsNeedsViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {


    @IBOutlet weak var collectionView: UICollectionView!
    let TAGS = ["Tech", "Design", "Humor", "Travel", "Music", "Writing", "Social Media", "Life", "Education", "Edtech", "Education Reform", "Photography", "Startup", "Poetry", "Women In Tech", "Female Founders", "Business", "Fiction", "Love", "Food", "Sports"]


    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate = self
        collectionView.dataSource = self

        let cellNib = UINib(nibName: "tagCell", bundle: nil)
        self.collectionView.registerNib(cellNib, forCellWithReuseIdentifier: "tagCell")
        self.collectionView.backgroundColor = UIColor.clearColor()


        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return TAGS.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        var tagCell = collectionView.dequeueReusableCellWithReuseIdentifier("tagCell", forIndexPath: indexPath) as! tagCell

        return tagCell
    }


}

Solution

  • You need to set the Reuse Identifier on your cell.

    Just open your storyboard, select the cell from the collection view and set it like the following:

    enter image description here

    UPDATE:

    Also, you must always name your classes using CamelCase. So change the class name, the .swift file name and wherever you wants to call the class call it TagCell. The broken line should now look like this:

    var tagCell = collectionView.dequeueReusableCellWithReuseIdentifier("tagCell", forIndexPath: indexPath) as! TagCell