gpt4 book ai didi

ios - UICollectionview 单元格选择

转载 作者:IT王子 更新时间:2023-10-29 08:10:29 24 4
gpt4 key购买 nike

我制作了一个图像网格,为了显示它的选择,我在选择时为图像绘制了边框。但问题是当我在顶部选择一些图像并向下滚动图像网格时,似乎也选择了底部的其他图像。下面是我的代码片段:

UINib *cellNib = [UINib nibWithNibName:@"collectionCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cellCV"];

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(95, 95)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

[self.collectionView setCollectionViewLayout:flowLayout];

上面已经在viewDidLoad中加入了在nib中设计的collection view cell。

并实现了以下委托(delegate):

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
selectedImageIndex = indexPath.row;
[collectionView reloadData];
}

-(CollectionCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *img = [imageArray objectAtIndex:indexPath.row];

static NSString *cellIdentifier = @"cellCV";
CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:
cellIdentifier forIndexPath:indexPath];
cell.imageView.image = img;
cell.imageView.tag = indexPath.row;
UIImageView *imgView = (UIImageView *)[cell viewWithTag:indexPath.row];
if (indexPath.row == selectedImageIndex) {
imgView.layer.borderWidth = 4.0;
imgView.layer.borderColor = [UIColor redColor].CGColor;
NSLog(@"selected indexpath: %d", indexPath.row);
}
else {
imgView.layer.borderWidth = 0.0;
imgView.layer.borderColor = nil;
}
return cell;
}

我猜想重复使用单元格会出现问题,但不确定也无法想出解决问题的办法。等待任何形式的帮助和建议。

提前致谢。

最佳答案

我不明白为什么会发生这种情况。我不认为问题出在 rowitem 的使用上,尽管您确实应该使用 item。不过,我可以想象,如果您的 Collection View 有多个 section,那么只查看 row/item 而忽略 section 将是一个问题(即它会在 every section 中选择相同的 item 编号)。

要快刀斩乱麻,我建议保存所选项目的 NSIndexPath,然后将其用作比较的基础。这也使得在 didSelectItemAtIndexPath 中呈现优化变得容易。不管怎样,首先定义你的属性:

@property (nonatomic, strong) NSIndexPath *selectedItemIndexPath;

然后实现cellForItemAtIndexPathdidSelectItemAtIndexPath:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";

CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

cell.imageView.image = ...

if (self.selectedItemIndexPath != nil && [indexPath compare:self.selectedItemIndexPath] == NSOrderedSame) {
cell.imageView.layer.borderColor = [[UIColor redColor] CGColor];
cell.imageView.layer.borderWidth = 4.0;
} else {
cell.imageView.layer.borderColor = nil;
cell.imageView.layer.borderWidth = 0.0;
}

return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// always reload the selected cell, so we will add the border to that cell

NSMutableArray *indexPaths = [NSMutableArray arrayWithObject:indexPath];

if (self.selectedItemIndexPath)
{
// if we had a previously selected cell

if ([indexPath compare:self.selectedItemIndexPath] == NSOrderedSame)
{
// if it's the same as the one we just tapped on, then we're unselecting it

self.selectedItemIndexPath = nil;
}
else
{
// if it's different, then add that old one to our list of cells to reload, and
// save the currently selected indexPath

[indexPaths addObject:self.selectedItemIndexPath];
self.selectedItemIndexPath = indexPath;
}
}
else
{
// else, we didn't have previously selected cell, so we only need to save this indexPath for future reference

self.selectedItemIndexPath = indexPath;
}

// and now only reload only the cells that need updating

[collectionView reloadItemsAtIndexPaths:indexPaths];
}

顺便说一句,请注意,我并不是在乱用 tag 属性(我认为它没有任何值(value))。另请注意,我没有重新加载整个 Collection View ,而是只重新加载选定的单元格(如果之前有一个选定的单元格,那么也会重新加载那个单元格),这应该会更有效率。

关于ios - UICollectionview 单元格选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18857167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com