如何在集合视图中单选单元格?

奇拉格·加杰尔(Chirag Gajjar)

这些代码可以正常工作,但是当我滚动集合视图时,还选择了另一个单元格,例如,有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;

}
阿尔贝托·斯坎皮尼(Alberto Scampini)

发生这种情况是因为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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在行集合视图中制作2个单元格

来自分类Dev

如何在所选单元格上添加自定义视图

来自分类Dev

如何在集合视图单元格中插入图像

来自分类Dev

如何在容器视图中更新单元格

来自分类Dev

如何在表视图中创建动态单元格?

来自分类Dev

如何反转集合视图单元格

来自分类Dev

呈现弹出视图时,如何让用户在父集合视图中选择单元格?

来自分类Dev

如何使集合视图中的不同单元格进入Swift中的不同视图控制器

来自分类Dev

如何通过快速点击集合视图的单元格在表视图中重新加载数据

来自分类Dev

集合视图中的动态高度单元格

来自分类Dev

集合视图中单元格的宽度不正确

来自分类Dev

集合视图中的动态高度单元格

来自分类Dev

集合视图中每个单元格的 NSFetchedResultsController

来自分类Dev

从集合视图中删除未选择的单元格

来自分类Dev

如何修复表格视图中各节中的单元格分离集合?

来自分类Dev

如何从集合视图中删除多个选定的单元格?(迅速)

来自分类Dev

如何找出在集合视图单元格内点击了哪个集合视图单元格

来自分类Dev

如何在集合视图单元格中更改图像视图

来自分类Dev

快速按下集合视图单元格时如何修复自定义视图中的约束

来自分类Dev

如何在集合视图单元格中更改图像

来自分类Dev

如何在表格视图中选择一个单元格而不滚动它?

来自分类Dev

如何在表格单元格视图中连接插座?

来自分类Dev

在Swift中,当定向发生时,如何在表格视图中看到相同的单元格?

来自分类Dev

如何在列表视图中更改单元格的基于行颜色的值

来自分类Dev

如何在Javafx(fxml)的表格视图中的单元格上单击鼠标右键?

来自分类Dev

如何在Xamarin Forms的列表视图中的多个项目中添加图像单元格

来自分类Dev

如何在表视图中添加额外的行,除了快速的两个单元格

来自分类Dev

如何在网格视图中选择特定的单元格值

来自分类Dev

如何在C#中的网格视图中获取选定单元格的值

Related 相关文章

  1. 1

    如何在行集合视图中制作2个单元格

  2. 2

    如何在所选单元格上添加自定义视图

  3. 3

    如何在集合视图单元格中插入图像

  4. 4

    如何在容器视图中更新单元格

  5. 5

    如何在表视图中创建动态单元格?

  6. 6

    如何反转集合视图单元格

  7. 7

    呈现弹出视图时,如何让用户在父集合视图中选择单元格?

  8. 8

    如何使集合视图中的不同单元格进入Swift中的不同视图控制器

  9. 9

    如何通过快速点击集合视图的单元格在表视图中重新加载数据

  10. 10

    集合视图中的动态高度单元格

  11. 11

    集合视图中单元格的宽度不正确

  12. 12

    集合视图中的动态高度单元格

  13. 13

    集合视图中每个单元格的 NSFetchedResultsController

  14. 14

    从集合视图中删除未选择的单元格

  15. 15

    如何修复表格视图中各节中的单元格分离集合?

  16. 16

    如何从集合视图中删除多个选定的单元格?(迅速)

  17. 17

    如何找出在集合视图单元格内点击了哪个集合视图单元格

  18. 18

    如何在集合视图单元格中更改图像视图

  19. 19

    快速按下集合视图单元格时如何修复自定义视图中的约束

  20. 20

    如何在集合视图单元格中更改图像

  21. 21

    如何在表格视图中选择一个单元格而不滚动它?

  22. 22

    如何在表格单元格视图中连接插座?

  23. 23

    在Swift中,当定向发生时,如何在表格视图中看到相同的单元格?

  24. 24

    如何在列表视图中更改单元格的基于行颜色的值

  25. 25

    如何在Javafx(fxml)的表格视图中的单元格上单击鼠标右键?

  26. 26

    如何在Xamarin Forms的列表视图中的多个项目中添加图像单元格

  27. 27

    如何在表视图中添加额外的行,除了快速的两个单元格

  28. 28

    如何在网格视图中选择特定的单元格值

  29. 29

    如何在C#中的网格视图中获取选定单元格的值

热门标签

归档