6

I have created a UIcollectionView and an array with some strings @[@"Item One", "Item Two", @"Item Three"];

In Tableview I would do this:

NSString *object = self.titlesArray[indexPath.row];
cell.textLabel.text = object;

But I can really not figure out how to do this for Items in UIcollectionView.

M. El-Set
  • 691
  • 4
  • 10
  • 15
  • A good place to start: http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12. Also Google is really helpful – fguchelaar Nov 21 '14 at 10:51

5 Answers5

15

UICollectionViewCell doesn’t have a default cell style. You have to create a custom UICollectionViewCell and add a UILabel inside.

Alex Cio
  • 5,752
  • 4
  • 40
  • 73
roborg
  • 223
  • 2
  • 9
4

UICollectionViewCell does not have default textLabel as UITableviewCell has, you have to create custom UICollectionViewCell as per your need.

You can look at this tutorial how to create custom collection view cell.

Yuvrajsinh
  • 4,301
  • 1
  • 14
  • 31
3

Swift 4.2

I came here because of the same issue but for Swift and I fixed it using this, I hope it helps:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
    let title = UILabel(frame: CGRect(x: 0, y: 0, width: cell.bounds.size.width, height: 50))
    title.text = "Some random text"
    title.font = UIFont(name: "AvenirNext-Bold", size: 15)
    title.textAlignment = .center
    cell.contentView.addSubview(title)
    return cell
}

Do not forget to register the cell:

collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
Joule87
  • 330
  • 1
  • 8
  • The issue with this approach is that you are creating a new label every time you dequeue a cell. Because the collection view is a recycling view it will eventually give you a cell that you have used before but that isn't currently displayed. This is for efficiency because it means it doesn't have to create a new cell all the time which is expensive. This code will then add another label on top of the previous label giving you a mess. It is probably better to create a subclass of UICollectionViewCell that has just a single text view in it. – leafcutter Nov 30 '20 at 15:21
1

I had the same question so I made a mini tutorial to do this: How to make a simple collection view with Swift. The code is in Swift but the process is about the same for Objective C.

The main steps are

  • Add a UICollectionView to the View Controller in the storyboard
  • Add UILabel to the Collection View Cell.
  • Make a custom subclass of UICollectionViewCell to hold the cell Label outlet.
  • Make the Collection View Cell use that class.
  • Implement the UICollectionViewDataSource and UICollectionViewDelegate and their methods in the View Controller.
  • Hook up all the outlets.
  • Use the strings in an array or other data source to populate the cells.
Community
  • 1
  • 1
Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198
0

Like the others already posted, there is property of type UILabel. If you don't know about a class, always press CMD and select the class you work on. In the case of UICollectionView you would see, there is no property defined (UICollectionViewCell class inside the Foundation:

NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionViewCell : UICollectionReusableView

@property (nonatomic, readonly) UIView *contentView; // add custom subviews to the cell's contentView

// Cells become highlighted when the user touches them.
// The selected state is toggled when the user lifts up from a highlighted cell.
// Override these methods to provide custom UI for a selected or highlighted state.
// The collection view may call the setters inside an animation block.
@property (nonatomic, getter=isSelected) BOOL selected;
@property (nonatomic, getter=isHighlighted) BOOL highlighted;

// The background view is a subview behind all other views.
// If selectedBackgroundView is different than backgroundView, it will be placed above the background view and animated in on selection.
@property (nonatomic, retain) UIView *backgroundView;
@property (nonatomic, retain) UIView *selectedBackgroundView;

@end
Alex Cio
  • 5,752
  • 4
  • 40
  • 73