无论在 UICollectionView 中选择什么 IndexPath,只有最后一个单元格被突出显示
Only The Last Cell Gets Highlighted No Matter What IndexPath is Selected in UICollectionView
发生这种事太疯狂了,我就是不明白。
我有一个充满图像的 UICollectionView。我希望能够 select 图像并在其周围绘制红色边框(或添加 x、更改 alpha 或对其执行任何操作)。但无论我做什么或将代码放在哪里,只有视图中的最后一张图像会出现红色边框。在 cell selection 之后一切都按预期执行 - (正确的图像被删除(正确的索引路径中的图像),collectionview 被重新加载,文档目录被更新)。为什么我不能突出显示正确的图像?
我确实有一个 TapGestureRecognizer 来实现绘制边框(如果这有所不同),但是当我通过按下按钮调用代码时它正在做同样的事情,在这种情况下,预计所有单元格都会得到一个红色边框,但这也没有发生。只有最后一个单元格有边框???
为了清楚起见,假设集合视图中有九张图片。我可以点击索引 0,但只有索引 8 会出现红色边框。如果我点击索引 1 到 8 本身也是一样,只有索引 8 会获得边界。
我尝试的另一件事是在每个单元格上设置一个 x 的图像,并在显示集合视图时隐藏它。当在 selected 索引处点击图像时,我试图取消隐藏它。同样的事情发生了。当点击一个单元格时,最后一个单元格得到 x 而不是 selected 单元格!什么鬼?
我所有的 NSLog 都表明正在读取正确的索引路径,并且在最终结果中得到证明,因为所有内容都已更新,但如果无法突出显示正确的单元格,用户体验会很差。
如果有任何关于这种荒谬的想法,我将不胜感激。
单元格以正常方式填充:
_cell.bookImageView.image = [_book.imageArray objectAtIndex:indexPath.row];
这是 cellForItemAtIndexPath 中的点击手势:
UITapGestureRecognizer *deleteImageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deleteImageTapped:)];
deleteImageTap.numberOfTapsRequired = 2;
_cell.bookImageView.tag = indexPath.row;
[_cell.bookImageView addGestureRecognizer:deleteImageTap];
_cell.bookImageView.userInteractionEnabled = YES;
这就是我想要做的:
_cell.layer.borderColor = [UIColor yellowColor].CGColor;
_cell.layer.borderWidth = 2;
甚至这样:
_cell.alpha = 0.4;
我尝试了几个地方的代码:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
-(void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
//my tap gesture method
-(void)deleteImageTapped:(UITapGestureRecognizer *)gesture
//how I get the indexpath in my method
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:gesture.view.tag inSection:0];
我很了解你的问题。
我建议您在 CellForRowAtIndexPath 中使用按钮,而不是 UiTapGesture。
首先在您的单元格中添加按钮。
cell.yourbutton.tag = indexPath.row;
[cell.yourbutton addTarget:self 动作:@selector(你的按钮被点击了:) forControlEvents:UIControlEventTouchUpInside];
-(void)yourButtonClicked:(UIButton*)sender
{
if (sender.tag == 0)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
}
}
事实证明,我只需要 "mention" 我的单元格在 didHighlightItemAtIndexPath 和 didUnhighlightItemAtIndexPath 中。但无论出于何种原因,向单元格添加更改从未在此处调用。
我将 UITapGestureRecognizer 留在我的 cellForItemAtIndexPath 中,并在从我的点击手势调用的方法中添加了对单元格的更改。
基本设置是这样的:
@property (nonatomic, strong) CollectionViewCell *cell;
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
_cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
if (!_cell.selected)
{
//this "turns off" the cell changes
_cell.alpha = 1.0;
_cell.deleteButton.alpha = 0.0;
_cell.bookImageView.layer.borderColor = nil;
_cell.bookImageView.layer.borderWidth = 0;
}
UITapGestureRecognizer *deleteImageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deleteImageTapped:)];
deleteImageTap.numberOfTapsRequired = 2;
_cell.bookImageView.tag = indexPath.row;
[_cell.bookImageView addGestureRecognizer:deleteImageTap];
_cell.bookImageView.userInteractionEnabled = YES;
return _cell;
}
-(void)deleteImageTapped:(UITapGestureRecognizer *)gesture
{
_cell.alpha = 0.4;
_cell.deleteButton.alpha = 1.0;
_cell.bookImageView.layer.borderColor = [UIColor redColor].CGColor;
_cell.bookImageView.layer.borderWidth = 5;
//more code to delete image blah blah blah
}
然后这是我"mention"我的手机
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
_cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
}
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
_cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
}
希望对大家有所帮助!
发生这种事太疯狂了,我就是不明白。
我有一个充满图像的 UICollectionView。我希望能够 select 图像并在其周围绘制红色边框(或添加 x、更改 alpha 或对其执行任何操作)。但无论我做什么或将代码放在哪里,只有视图中的最后一张图像会出现红色边框。在 cell selection 之后一切都按预期执行 - (正确的图像被删除(正确的索引路径中的图像),collectionview 被重新加载,文档目录被更新)。为什么我不能突出显示正确的图像?
我确实有一个 TapGestureRecognizer 来实现绘制边框(如果这有所不同),但是当我通过按下按钮调用代码时它正在做同样的事情,在这种情况下,预计所有单元格都会得到一个红色边框,但这也没有发生。只有最后一个单元格有边框???
为了清楚起见,假设集合视图中有九张图片。我可以点击索引 0,但只有索引 8 会出现红色边框。如果我点击索引 1 到 8 本身也是一样,只有索引 8 会获得边界。
我尝试的另一件事是在每个单元格上设置一个 x 的图像,并在显示集合视图时隐藏它。当在 selected 索引处点击图像时,我试图取消隐藏它。同样的事情发生了。当点击一个单元格时,最后一个单元格得到 x 而不是 selected 单元格!什么鬼?
我所有的 NSLog 都表明正在读取正确的索引路径,并且在最终结果中得到证明,因为所有内容都已更新,但如果无法突出显示正确的单元格,用户体验会很差。
如果有任何关于这种荒谬的想法,我将不胜感激。
单元格以正常方式填充:
_cell.bookImageView.image = [_book.imageArray objectAtIndex:indexPath.row];
这是 cellForItemAtIndexPath 中的点击手势:
UITapGestureRecognizer *deleteImageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deleteImageTapped:)];
deleteImageTap.numberOfTapsRequired = 2;
_cell.bookImageView.tag = indexPath.row;
[_cell.bookImageView addGestureRecognizer:deleteImageTap];
_cell.bookImageView.userInteractionEnabled = YES;
这就是我想要做的:
_cell.layer.borderColor = [UIColor yellowColor].CGColor;
_cell.layer.borderWidth = 2;
甚至这样:
_cell.alpha = 0.4;
我尝试了几个地方的代码:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
-(void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
//my tap gesture method
-(void)deleteImageTapped:(UITapGestureRecognizer *)gesture
//how I get the indexpath in my method
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:gesture.view.tag inSection:0];
我很了解你的问题。 我建议您在 CellForRowAtIndexPath 中使用按钮,而不是 UiTapGesture。
首先在您的单元格中添加按钮。
cell.yourbutton.tag = indexPath.row;
[cell.yourbutton addTarget:self 动作:@selector(你的按钮被点击了:) forControlEvents:UIControlEventTouchUpInside];
-(void)yourButtonClicked:(UIButton*)sender
{
if (sender.tag == 0) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
}
}
事实证明,我只需要 "mention" 我的单元格在 didHighlightItemAtIndexPath 和 didUnhighlightItemAtIndexPath 中。但无论出于何种原因,向单元格添加更改从未在此处调用。
我将 UITapGestureRecognizer 留在我的 cellForItemAtIndexPath 中,并在从我的点击手势调用的方法中添加了对单元格的更改。
基本设置是这样的:
@property (nonatomic, strong) CollectionViewCell *cell;
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
_cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
if (!_cell.selected)
{
//this "turns off" the cell changes
_cell.alpha = 1.0;
_cell.deleteButton.alpha = 0.0;
_cell.bookImageView.layer.borderColor = nil;
_cell.bookImageView.layer.borderWidth = 0;
}
UITapGestureRecognizer *deleteImageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deleteImageTapped:)];
deleteImageTap.numberOfTapsRequired = 2;
_cell.bookImageView.tag = indexPath.row;
[_cell.bookImageView addGestureRecognizer:deleteImageTap];
_cell.bookImageView.userInteractionEnabled = YES;
return _cell;
}
-(void)deleteImageTapped:(UITapGestureRecognizer *)gesture
{
_cell.alpha = 0.4;
_cell.deleteButton.alpha = 1.0;
_cell.bookImageView.layer.borderColor = [UIColor redColor].CGColor;
_cell.bookImageView.layer.borderWidth = 5;
//more code to delete image blah blah blah
}
然后这是我"mention"我的手机
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
_cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
}
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
_cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
}
希望对大家有所帮助!