这些代码可以正常工作,但是当我滚动我的 Collection View 时,还会选择另一个单元格,例如,有 18 个图像可用,运行时首先显示 6 个,然后我将选择位置中的任何一个,然后选择接下来的 6 个位置图像自动选择。为什么一次选择两个单元格我在这里感到困惑。请给我解决方案
这里我在主要节目 Storyboard上拍摄了 6 个单元格
UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout;
flowLayout.minimumLineSpacing = 15;
CGFloat availableWidthForCells = CGRectGetWidth(self.collectionView.frame) - flowLayout.sectionInset.left - flowLayout.sectionInset.right - flowLayout.minimumInteritemSpacing *2;
cellWidth = availableWidthForCells /6;
NSLog(@"cellWidth:%f",cellWidth);
flowLayout.itemSize = CGSizeMake(cellWidth, cellWidth);
这是我的 Didselect 和 didDeselect 方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.layer.cornerRadius = cellWidth / 2.0;
cell.layer.backgroundColor = [UIColor blackColor].CGColor;
NSLog(@"INDEXPATH:-%ld",(long)indexPath.row);
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.layer.cornerRadius = cellWidth / 2.0;
cell.layer.backgroundColor = [UIColor whiteColor].CGColor;
}
最佳答案
这是因为 collectionView 重用了单元格;
您应该将所选单元格的 IndexPath 存储在一个变量中:
对象:
@property (nonatomic, retain) NSIndexPath *selectedIndexPath;
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.layer.backgroundColor = [UIColor blackColor].CGColor;
NSLog(@"INDEXPATH:-%ld",(long)indexPath.row);
self.selectedIndexPath = indexPath
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.layer.backgroundColor = [UIColor whiteColor].CGColor;
self.selectedIndexPath = nil
}
swift :
var selectedIndexPath: IndexPath?
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell.layer.backgroundColor = UIColor.black
self.selectedIndexPath = indexPath
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell.layer.backgroundColor = UIColor.white
self.selectedIndexPath = nil
}
比在“indexPath 处的行的单元格”中检查:
对象:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.layer.cornerRadius = cellWidth / 2.0;
if (self.selectedIndexPath != nil && indexPath == self.selectedIndexPath) {
cell.layer.backgroundColor = [UIColor blackColor].CGColor;
else {
cell.layer.backgroundColor = [UIColor whiteColor].CGColor;
}
return cell
}
swift :
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.cellForItem(at: indexPath)
cell.layer.cornerRadius = cellWidth / 2
if self.selectedIndexPath != nil && indexPath == self.selectedIndexPath {
cell.layer.backgroundColor = UIColor.black
else {
cell.layer.backgroundColor = UIColor.white
}
}
关于ios - 如何在 Collection View 中单选单元格..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36861137/