将 UICollectionView 添加到 UIView

Add UICollectionView to a UIView

我已经尝试搜索与此问题相关的 Stack Overflow 问题,但似乎无法理解我做错了什么。我有一个 UIView 是一个 xib。我将其用作地图注释标注。我想在这个标注 xib 中有一个 UICollectionView 。所以我在 xib 中添加了一个 UICollectionView 并且因为它不允许在 UIView 内部时将单元格添加到 UICollectionView 我为我创建了自己的 class带有 xib 的单元格。

我一直崩溃 '*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:]'.

我目前将 UIView 作为 collectionView 的数据源和委托。

提前致谢。

 //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;

}

我发现了问题所在。在为地图标注加载笔尖之前,我正在为 UICollectionViewCell 注册笔尖。因此,在建立包含 UICollectionView 的标注之前,它试图创建单元格并将其放入 UICollectionView 中。此外,我在单元格上为我的 UIImageView 连接了一个插座,并试图以这种方式设置图像。相反,我只是给 imageView 一个标签,并在 'cellForItemAtIndexPath' 方法中使用该标签设置图像。现在效果很好。

-(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;
}