UICollectionView
的设置使用 IB 定义(即滚动方向:水平等),并嵌入到 UITableViewCell
中使用 IB。
UICollectionViewCell
显示器,图像显示,但是,图像堆叠在彼此之上,而不是一个图像一个图像 cell
保真度。
我做了个人UIImageView
对于每张图片作为实例变量,并且使用 if 和 switch
发生相同的情况cellForItemAtIndexPath
中的陈述消息。
由于使用了IB,所以识别bug可能有点困难,但是,如果从代码中可以看出bug,请您帮忙识别一下吗?谢谢。
@implementation AccountTableViewCell - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state imageArray = @[[UIImage imageNamed:@"image1.png"], [UIImage imageNamed:@"image2.png"], [UIImage imageNamed:@"image3.png"], [UIImage imageNamed:@"image4.png"], [UIImage imageNamed:@"image5.png"]]; self.oCollectionView.dataSource = self; [self.oCollectionView setFrame:self.contentView.frame]; [self.contentView addSubview:self.oCollectionView]; self.oCollectionView.backgroundColor = [UIColor clearColor]; [self.oCollectionView reloadData]; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return imageArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"accountCell" forIndexPath:indexPath]; UIImageView* iv = [[UIImageView alloc] init]; [cell.contentView addSubview:iv]; [iv setFrame:cell.contentView.frame]; iv.image = imageArray[indexPath.row]; return cell; } @end
最佳答案
这是因为你一直在添加一个 UIImageView
到 cell
每次它出列。
相反,您应该子类化 UICollectionViewCell
(我们称它为“MYCollectionViewCell”,在UIImageView
中的cell
子类中添加一个storyboard
,并将UIImageView
设置为子类上的导出。
然后,在cellForItemAtIndexPath
内, 设置 imageView's
像这样的图像:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"accountCell" forIndexPath:indexPath];
cell.imageView.image = imageArray[indexPath.row];
return cell;
}
关于objective-c - UITableViewCell 错误中的 UICollectionViewCell 中的 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18967197/