【问题标题】:IndexPath for Selected Cell UICollectionView选定单元格 UICollectionView 的 IndexPath
【发布时间】:2015-05-27 10:55:56
【问题描述】:

我目前正在开发一个显示各种单元格的 UICollectionView ...

到目前为止一切顺利,但是我对单元格的选择有问题......例如,我实现了这样的选择方法:

    - (Void)collectionView:(UICollectionView *) collectionView didSelectItemAtIndexPath:(NSIndexPath *) indexPath {
        

          UICollectionViewCell theCell * = (UICollectionViewCell *) [CollectionView cellForItemAtIndexPath: indexPath];

         theCell.backgroundColor = [UIColor orangeColor];
        
}

问题在于,当我浏览集合视图时,除了使选定单元格成为橙色之外,还会创建其他选择..

示例:选择 7 号单元格并自动选择 14 号单元格......显然我不需要这个,但我希望它只选择 7 号单元格

我哪里做错了?我的 indexPath 有问题吗?

【问题讨论】:

    标签: ios8 uicollectionview nsindexpath


    【解决方案1】:

    在方法中

    (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    

    像这样添加:

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"yourCelId" forIndexPath:indexPath];
    
    
    if (cell.selected) {
         cell.backgroundColor = [UIColor orangeColor]; 
    } else {
         cell.backgroundColor = [UIColor clearColor];
    }   
    

    这是因为单元格被重复使用,您必须重置所有可以更改的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-05
      • 2015-11-21
      • 1970-01-01
      • 1970-01-01
      • 2019-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多