-1

i am trying to create collectionView but it creates error as: use of undeclared identifier in collectionView.

- (UICollectionView *)collectionView:(UICollectionView *)collectionView
               cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  UICollectionViewCell *cell =
      [collectionView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

  if (cell == nil) {
    cell = [[UICollectionViewCell alloc] init];

    [UICollectionView setBackgroundColor:[UIColor redColor]];
  }

  [self.view addSubview:__collectionView];

  // Do any additional setup after loading the view, typically from a nib.
}
Kirit Modi
  • 21,371
  • 13
  • 83
  • 108
Mano Chitra R
  • 211
  • 1
  • 3
  • 12

2 Answers2

0

Registe your nib in view Did load method...

UINib *cellNib = [UINib nibWithNibName:@"NibCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];

now run your app...

Chandan
  • 1,046
  • 12
  • 30
  • Error in this line - (UICollectionView *)collectionView:(UICollectionView *)collectionView cellForRowAtIndexPath:(NSIndexPath *)indexPath – Mano Chitra R Feb 20 '15 at 06:31
  • NO plz add your nib file (Cell ) in method view did load in .m file .. for more info ... http://stackoverflow.com/questions/17856055/creating-a-uicollectionview-programmatically – Chandan Feb 20 '15 at 06:33
  • yes it come bcs you don't register your cell so first register cell ... visit url may help you......http://adoptioncurve.net/archives/2012/09/a-simple-uicollectionview-tutorial/ – Chandan Feb 20 '15 at 06:35
0

If you are not using prototypeCell in the storyboard. You could either register Cell/Xib with the collectionView. Do this in viewDidLoad

[self.collectionView registerClass:[UICollectionViewCell class]
forCellWithReuseIdentifier: @"MyIdentifier"];
Anil Varghese
  • 41,329
  • 9
  • 90
  • 110