【问题标题】:UIImageView in UICollectionViewCell in UITableViewCell bugUITableViewCell 中的 UICollectionViewCell 中的 UIImageView 错误
【发布时间】:2013-09-28 19:38:33
【问题描述】:

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

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

我为每张图片创建了单独的 UIImageView 作为实例变量,并且在 cellForItemAtIndexPath 消息中使用 if 和 switch 语句也发生了同样的情况。

由于使用了 IB,因此识别错误可能会有些困难,但是,如果从代码中很明显,请您帮忙识别错误吗?谢谢。

@implementation AccountTableViewCell

- (void)setSelected:(BOOL)选定动画:(BOOL)动画
{
    [超级setSelected:选定动画:动画];

    // 配置选中状态的视图

    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
{
    返回 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    返回 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];

    返回单元格;
}

@end

【问题讨论】:

    标签: objective-c uitableview uiimageview uicollectionviewcell


    【解决方案1】:

    这是因为每次出队时,您都会不断地将UIImageView 添加到cell

    相反,您应该将UICollectionViewCell 子类化(我们称之为“MYCollectionViewCell”,将UIImageView 添加到storyboard 中的cell 子类,并将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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多