Set text in uicollectionviewcell
UICollectionViewCell
doesn’t have a default cell style.
You have to create a custom UICollectionViewCell
and add a UILabel
inside.
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.
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")