-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
  • 23,155
  • 15
  • 89
  • 112
Mano Chitra R
  • 231
  • 1
  • 3
  • 13

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 kumar
  • 1,074
  • 12
  • 31
  • 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 kumar 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 kumar 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
  • 42,757
  • 9
  • 93
  • 110