'Set text in uicollectionviewcell

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.



Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[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")

Solution 4:[4]

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.

Solution 5:[5]

Since the iOS 14 it is possible to use UICollectionViewListCell class which has this text setting option. Apple docs.

var content = cell.defaultContentConfiguration()

// Configure content.
content.text = "Hello"

cell.contentConfiguration = content

Solution 6:[6]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex Cio
Solution 2
Solution 3 Joule87
Solution 4 Community
Solution 5 Evgeny Karkan
Solution 6 Alex Cio