ios - 向上向下滚动时 collectionViewCell 图像发生变化

标签 ios objective-c iphone uitableview uicollectionviewcell

我正在尝试创建一个具有 collectionviewtableviewcell,该 collectionview 具有自定义 collectionviewcell。 我创建了一个 Controller ,具有自定义 tableviewcell。 TableViewCell 嵌入了 collectionview。现在我创建了一个带有imagetext 的自定义collectioncell

collectionViewcell 中,第 4 个图像需要是半透明的,所以当我在 cellforitematindex 中渲染图像时,我将图像设为半透明。在我向下和向上滚动表格 View 之前,它完全可以正常工作。在我们向上滚动后,第一张图片变成半透明的。

我在网上看到 collectionviewcell 可以重用,可能是第 4 个重用在第一个位置,第一个位置得到半透明图像

我有一个 Controller 类,它是 tableviewcellcollectionviewcell 的数据源和委托(delegate)。

I have tableviewcell class.
I have collectionviewcell class.

现在我得到一个链接来解决这个问题,它说你必须子类化你的 collectionviewcell 并添加一些方法使 imageview 每次都为 nil 并且它会创建一个新的 ImageView

Link for the above

所以我创建了一个collectionviewcell子类

I have a subclass of collectionviewcell : uicollectionviewsubclass

在我的 Controller 中,首先我重用了tablecell

所以我创建了一个 Controller 让我们说:-

mycontroller 并添加了一个 tableview ,我添加了一个自定义 tableviewcell , 在 cellForRowAtIndexPath 我 dequeueReusableCellWithIdentifier ,所以我的 tableviewcell 会来。现在在我的 tableViewcell .m 文件中,我为 collectionview 创建了一个导出,并通过使用我制作的标签

self.collectionview = [self.contentView viewWithTag:tagno];

This tag is a collectionviewcell tagno

现在我在 tableviewcell 类中注册了 collectionview ,它有 collectionview 的导出

[self.collectionview registerClass:[uicollectionviewsubclass class] forCellWithReuseIdentifier:@"reuseidentifier"];

在我的 collectionviewcell 中,我有 imageview socket 和其他 socket

当我创建子类时,我创建的就像链接中所说的那样。

然后在我的 cellForItemAtIndexPath Controller 中为 collectionview

uicollectionviewsubclass *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"reuseidentifier" forIndexPath:indexPath];

但是这个单元格是一个子类单元格,它没有 imageview 的导出(whihc 在 collectionview 类中) 因此它在自身中显示 imageView 为 nil 未实例化。

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

     ImageCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];
    cell.ImageCellView.image  = [UIImage imageWithData: imageData];
    if (indexPath.row == 3) {
            cell.grayViewImage.hidden = FALSE;
    }
    return cell;
}

grayViewImage is a uiview which i make hidden false to make the cell translucent

可能是什么原因。

最佳答案

要解决此问题,您应该准备好要重复使用的单元格。为 UICollectionViewCellUITableViewCell 使用名为 prepareForReuse 的方法。 像这样实现它并在您的 cellForItemAtIndexPath

中调用它
- (void)prepareForReuse 
{
    yourImageView .image = nil;
    [super prepareForReuse];
}

关于ios - 向上向下滚动时 collectionViewCell 图像发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37317606/

相关文章:

ios - 在配置文件中将新版本上传到苹果商店时出错

ios - swift 将多个独立项目居中( Storyboard)

ios - 对于我不合成的属性,我是否需要 ARC 关键字?

iphone - 呈现模态视图总是失败

ios - 如何为自定义 UIWindow 实现我自己的 getter 方法?

iphone - iOS 6 的 Paypal 问题

iphone - 按人员在公司中的角色对人员进行排序

objective-c - 如何声明和使用 NSString 全局常量

ios - 在 watchkit 扩展和 watch app .plist 文件中设置 ATS key

ios - iOS 8 中的 shouldAutorotate 行为