这些代码可以正常工作,但是当我滚动集合视图时,还选择了另一个单元格,例如,有18张图像可用,并且在运行时首先显示6张图像,此时我将选择任意一个位置,然后自动选择下6个位置图像。为什么一次选择两个单元格使我感到困惑。请给我解决方案
在这里,我在主节目情节提要板上占据了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
}
斯威夫特:
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
}
斯威夫特:
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
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句