0

I've tried searching through the Stack Overflow questions related to this issue but can't seem to understand what I'm doing wrong. I have a UIView that is an xib. I use this as a map annotation callout. I want to have a UICollectionView inside of this callout xib. So I added a the UICollectionView to the xib and since it doesn't allow for the adding of a cell onto the UICollectionView when its inside of a UIView I created my own class for my cell with an xib for the cell.

I keep crashing with '*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:]'.

I currently have my UIView as the collectionView's dataSource and delegate.

Thanks in advance.

 //UIView
 **MapAnnotationCallout.m File**

    -(id)initWithFrame:(CGRect)frame {

 //'mediaPreviewCell' is my custom cell
[_mediaCollectionView registerNib:[UINib nibWithNibName:@"MediaPreviewCell" bundle:nil] forCellWithReuseIdentifier:@"mediaCell"];

_mediaCollectionView.backgroundColor = [UIColor clearColor];

self = [super initWithFrame:frame];
if (self) {

    _previewImages = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"belleIsle-1"], [UIImage imageNamed:@"belleIsle-2"], [UIImage imageNamed:@"old_english_D"], nil];

    [[NSBundle mainBundle]loadNibNamed:@"MapAnnotationCalloutView" owner:self options:nil];

    self.bounds = _view.bounds;

    [self addSubview:_view];

}
return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [_previewImages count];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

return CGSizeMake(80, 80);
}

- (MediaPreviewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

 *******crashing on this line with exception breakpoint************

MediaPreviewCell *cell = [_mediaCollectionView dequeueReusableCellWithReuseIdentifier:@"mediaCell" forIndexPath:indexPath];

 ******************************************    

cell.cellImage.image = [_previewImages objectAtIndex:indexPath.item];

return cell;

}
dcotter
  • 312
  • 6
  • 22
  • did you see this? http://stackoverflow.com/a/12600019/5716449 – Ro4ch Jul 16 '16 at 20:40
  • @Ro4ch Thanks for the response. Yes I did. I am registering the nib in the initWithFrame method. – dcotter Jul 16 '16 at 20:49
  • What is the assertion that fails? – matt Jul 16 '16 at 21:03
  • Why would anyone ever say `self.bounds = _view.bounds; [self addSubview:_view];`? The bounds of the superview are adjusted to the bounds of the subview????? – matt Jul 16 '16 at 21:07
  • @matt Hey first time doing this but the explanation given from the tutorial I followed was that the _view.bounds is what is being loaded from the xib file that I have and that this helps with the with the positioning of my views such as my labels, UIImageViews, etc inside of the custom UIView. Do you have a better explanation or a suggestion on a better way to do this? – dcotter Jul 16 '16 at 21:43
  • @matt the assertion that is failing seems to be when it tries to insert my custom cell (nib) into my collection view – dcotter Jul 16 '16 at 21:44
  • I understand when it is. But _what_ is it? An assertion comes with a big long message explaining what happened. Provide that. – matt Jul 16 '16 at 22:47

1 Answers1

0

I found out what the issue was. I was registering the nib for the UICollectionViewCell before I was loading the nib for the map callout. So it was trying to create the cell and put it in the UICollectionView before the callout that contains the UICollectionView was established. Also I had an outlet hooked up for my UIImageView on the cell and was trying to set the image that way. Instead I just gave the imageView a tag and set the image with that in the 'cellForItemAtIndexPath' method. This works great now.

-(id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];
if (self) {

     _previewImages = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"belleIsle-1"], [UIImage imageNamed:@"belleIsle-2"], [UIImage imageNamed:@"old_english_D"], nil];

    [[NSBundle mainBundle]loadNibNamed:@"MapAnnotationCalloutView" owner:self options:nil];

    [_mediaCollectionView registerNib:[UINib nibWithNibName:@"MediaPreviewCell" bundle:nil] forCellWithReuseIdentifier:@"mediaCell"];


    _mediaCollectionView.backgroundColor = [UIColor clearColor];

    self.bounds = _view.bounds;

    [self addSubview:_view];

   }
  return self;
}


- (MediaPreviewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

MediaPreviewCell *cell = [_mediaCollectionView dequeueReusableCellWithReuseIdentifier:@"mediaCell" forIndexPath:indexPath];

UIImageView *mediaImageView = (UIImageView *)[cell viewWithTag:100];

mediaImageView.image = [_previewImages objectAtIndex:indexPath.item];

return cell;
}
dcotter
  • 312
  • 6
  • 22