6

I have searched a lot for creating a UICollectionView programatically but none of them suggest the simplest way to use it, how to add a label or an image to the UICollectionViewCell. Most of the sites suggest that implementation of UICollectionView is same as UITableView, but the major difference comes when we try to add any image. In UITableView we can allocate the imageViews in cellForRow method where cell == nil and assign images where (cell != nil). but here in case of UICollectionView ItemAtIndexPath method, there is no condition (cell == nil) as in UITableView's CellForRow. As a result we can't effectively allocate variables of UImageViews or Labels etc in itemAtIndexPath method. I Want to know whether there is any alternative other than subclassing the UICollectionViewCell and allocating variables in that custom Class? Can any one help, any help is appreciated.

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Mounika Vangala
  • 396
  • 2
  • 3
  • 13
  • Maybe this blog post and the corresponding github repository I did helps you: http://dasdev.de/2013/12/27/delegate-and-data-source-of-collection-views/ – dasdom Nov 26 '14 at 11:25
  • ya to some extent but we have to subclass the NSObject in the link u suggested. So To add a label, theres no otherway other than subclassing which is not needed in uitableview class. Thanks anyway. – Mounika Vangala Nov 26 '14 at 11:32
  • http://stackoverflow.com/questions/17856055/creating-a-uicollectionview-programmatically – johny kumar Nov 26 '14 at 11:34

2 Answers2

3

There is not alternative to create or allocate cells in itemAtIndex method. We need to register the customised class to create any views inside the custom class. something like this :

[UICollectionView registerClass:[CustomCollectionViewClass class] forCellWithReuseIdentifier:@"cellIdentifier"];

here is the best link which I found useful. Hope it helps others

Dharman
  • 21,838
  • 18
  • 57
  • 107
Mounika Vangala
  • 396
  • 2
  • 3
  • 13
1

swift :

   override func viewDidLoad() {
       super.viewDidLoad()

       let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
       layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
       layout.itemSize = CGSize(width: 70, height: 70)

       let demoCollectionView:UICollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
       demoCollectionView.dataSource = self
       demoCollectionView.delegate = self
       demoCollectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
       demoCollectionView.backgroundColor = UIColor.whiteColor()
       self.view.addSubview(demoCollectionView)
   }

   func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return 27
   }

   func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
       let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
       cell.backgroundColor = UIColor.lightGrayColor()
       return cell
   }

   func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
  {
       print("User tapped on item \(indexPath.row)")
   }
Tanvir Nayem
  • 654
  • 8
  • 23