[英]UICollectionview cell selection
我制作了一個圖像網格,為了顯示其選擇,我在選擇時為圖像繪制了邊框。 但問題是當我在頂部選擇一些圖像並向下滾動圖像網格時,底部的其他一些圖像似乎也被選中。 下面是我的代碼片段:
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。
並實施以下代表:
-(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;
}
我可以猜到重復使用單元格會出現問題,但不確定並且無法解決問題。 等待任何形式的幫助和建議。
提前致謝。
我不明白為什么會發生這種情況。 我不相信問題是使用row
與item
,雖然你真的應該使用item
。 但是,我可以想象,如果你的集合視圖有多個section
,那么僅查看row
/ item
但忽略section
將是一個問題(即它將在每個 section
選擇相同的item
編號)。
為了減少Gordian結,我建議保存所選項目的NSIndexPath
,然后使用它作為比較的基礎。 這也使得在didSelectItemAtIndexPath
呈現優化變得容易。 無論如何,首先定義你的財產:
@property (nonatomic, strong) NSIndexPath *selectedItemIndexPath;
然后實現cellForItemAtIndexPath
和didSelectItemAtIndexPath
:
- (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
屬性(我認為沒有價值)。 另請注意,我不是重新加載整個集合視圖,而是僅重新加載所選單元格(如果還有一個先前選定的單元格,那也應該更高效)。
這段代碼在Swift 3中:
if self.selectedItemIndexPath != nil && indexPath.compare(self.selectedItemIndexPath) == .orderedSame {
cell.imageView!.layer.borderColor = UIColor.red.cgColor
cell.imageView!.layer.borderWidth = 4.0
} else {
cell.imageView!.layer.borderColor = nil
cell.imageView!.layer.borderWidth = 0.0
}
return cell
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.