objective-c - UITableViewCell 错误中的 UICollectionViewCell 中的 UIImageView

标签 objective-c uitableview uiimageview uicollectionviewcell

UICollectionView 的设置使用 IB 定义(即滚动方向:水平等),并嵌入到 UITableViewCell 中使用 IB。

UICollectionViewCell显示器,图像显示,但是,图像堆叠在彼此之上,而不是一个图像一个图像 cell保真度。

images stacked on top of one another

我做了个人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

最佳答案

这是因为你一直在添加一个 UIImageViewcell每次它出列。

相反,您应该子类化 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/

相关文章:

ios - 展开 UITableViewCell

iphone - 如何设置 TableView 单元格附件 View 以保留先前初始化的 UIImageView?

ios - 在 Swift 中制作 Gif 动画

ios - 作为选择器传递给 UI 测试框架的字符串意味着什么

c++ - 类似于 C++ 中的 Objective-C KVO

objective-c - 检查 NSStatusItem 当前是否显示在 OS X NSStatusBar 主菜单栏中

ios - 数据未显示在 UITableViewCell Swift 上

objective-c - 静态库不想使用.h文件只想使用.a文件

ios - 应用程序由于未捕获的异常“NSRangeException”而终止

ios - 覆盖 iPhone 中所有设备屏幕所需的图像大小