ios - 如何在 Collection View 中单选单元格..?

标签 ios uicollectionview

这些代码可以正常工作,但是当我滚动我的 Collection View 时,还会选择另一个单元格,例如,有 18 个图像可用,运行时首先显示 6 个,然后我将选择位置中的任何一个,然后选择接下来的 6 个位置图像自动选择。为什么一次选择两个单元格我在这里感到困惑。请给我解决方案

enter image description here

这里我在主要节目 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/

相关文章:

ios - UICollectionVIew Compositional Layout & DiffableDataSource,如何开启数据预取?

ios - 计时器在 Viper 架构中的什么位置

ios - UICollectionViewCell 响应多个手势识别器

ios 5 和 ios6 兼容性功能 -xcode

android - 使用超声波确定 2 个移动设备的相对位置

ios - 检查单元格是否完全显示在 Collection View 中

ios - 如何将数据传递到嵌入在TableViewCell中的UICollectionView(xCode-iOS-Swift 3)

ios - UICollectionView 在启用分页的情况下不会滚动完整的 320 点

ios - 尝试在已经呈现 UIAlertController 的 ViewController 上呈现 UIViewController

ios - UITableViewController 中的 UIActivityIndi​​catorView 与 UITableView 在同一层次结构中