【发布时间】:2015-06-05 10:40:30
【问题描述】:
我有一个带有自定义 UICollectionViewCell 的自定义 UICollectionView,我的单元格上有一个类似按钮(已连接到 CustomCVCell.h 文件),我需要在按下此按钮时更改它的背景。我所做的是在cellForItemAtIndexPath: 方法上声明按钮的操作,如下所示:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
FolderProducts *item = _feedItems[indexPath.item];
[cell.like addTarget:self action:@selector(likeProduct:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
然后在按钮操作上,我尝试像这样更改背景:
- (void)likeProduct:(UIButton *)button {
[button setImage:[UIImage imageNamed:@"dislike.png"] forState:UIControlStateNormal];
}
它可以工作,但是其他随机单元格的类似按钮的图像发生了变化,我不明白为什么..
我还尝试使用以下方法检索正确的单元格:
CollectionViewCell *cell = (CollectionViewCell *)button.superview.superview;
然后:
[button setImage:[UIImage imageNamed:@"dislike.png"] forState:UIControlStateNormal];
但结果还是错了
【问题讨论】:
标签: objective-c uicollectionview uicollectionviewcell