2

I have a class file that is a subclass of the UICollectionViewController class. In my cellForItemAtIndexPath method, I am trying to set the textLabel of the cells. However, there is no textLabel option in the completions. This is the method so far:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
    // Configure the cell
    cell.backgroundColor = cellColor ? UIColor.blueColor() : UIColor.orangeColor()
    //cell.textLabel.text = "Text"???
    switch indexPath.item {
    case 0...5:
        cellColor = true
    case 6...13:
        cellColor = false
    default:
        cellColor = true
    }
}

What can I do to add the textLabel to the cells?

EthanW
  • 63
  • 1
  • 4

3 Answers3

6

Unlike tableViewCell ,UICollectionViewCell doesn't have textLabel. You need to add the UILabel to cell's content view example:

var title = UILabel(frame: CGRectMake(0, 0, cell.bounds.size.width, 40))
cell.contentView.addSubview(title)
Ezimet
  • 5,058
  • 4
  • 23
  • 29
  • That looks like objective c, which I've never used. Could you convert that into swift? – EthanW Dec 17 '14 at 02:02
  • Sorry if I'm getting on your nerves-is there any way I can make the text a larger size, horizontally center, and vertically center the label in the cell? Thanks very much. – EthanW Dec 17 '14 at 02:22
  • It is very basic stuff actually , you really need to have understanding about these basics. You can make your text larger with font property label.font = UIFont(name: label.font.fontName, size: 20) you set the size value larger – Ezimet Dec 17 '14 at 02:27
  • one way of centering : title.center = cell.center – Ezimet Dec 17 '14 at 02:30
  • Ok, thank you. I should research more about these font properties. – EthanW Dec 17 '14 at 02:31
2

Swift 4

Here is the Swift 4 way to add text to a collection view cell:

    let title = UILabel(frame: CGRect(x: 0, y: 0, width: cell.bounds.size.width, height: 40))
    title.textColor = UIColor.black
    title.text = "T"
    title.textAlignment = .center
    cell.contentView.addSubview(title)
elarcoiris
  • 1,914
  • 4
  • 28
  • 30
1

Use the documentation, Luke. It's under Xcode's "Help" menu.

UICollectionViews don't have textLabels - that's why you don't get a completion.

They have contentViews. You can put text in the content view though. Any UIView subclass will do. If all you want is text (no graphics), then you could use a UITextView for your content view, then assign to its "text" property:

cell.contentView.text = "Text"

However, you will want a subclass of UICollectionView that already provides the UITextViews for the contentViews.

Mike Crawford
  • 2,232
  • 2
  • 18
  • 28
  • the only 2 Outlets for `UICollectionViewCell` are: `backgroundView` and `selectedBackgroundView` in storyboard. Can `contentView` be assigned via storyboard? – Raptor Dec 23 '15 at 07:36