【问题标题】:iOS UICollectionView cell selection per sectioniOS UICollectionView 每个部分的单元格选择
【发布时间】:2013-03-12 21:53:08
【问题描述】:

我想知道如何设置我的UICollectionView,以便每个部分最多可以选择 1 个单元格。我看到 UICollectionView 有 allowsMultipleSelection 属性,但我想知道如何防止在同一部分中选择多个单元格。

我需要在– collectionView:shouldSelectItemAtIndexPath:collectionView:shouldDeselectItemAtIndexPath: 方法中实现逻辑还是有更简单的方法?

谢谢!

【问题讨论】:

    标签: iphone ios uicollectionview uicollectionviewcell


    【解决方案1】:

    在您的 didSelectRowAtIndexPath 中,您可以执行以下操作:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        NSArray * selectedRows = self.tableView.indexPathsForSelectedRows;
    
        for (NSIndexPath * selectedRow in selectedRows) {
            if ((selectedRow.section == indexPath.section) && (selectedRow.row != indexPath.row)) {
                [self.tableView deselectRowAtIndexPath:selectedRow animated:NO];
            }
        }
    }
    

    allowsMultipleSelection 应设置为 YES。

    希望对你有帮助!

    【讨论】:

    • 感谢您的帮助。我不得不向它添加一行... if (selectedRow.row != indexPath.row) [collectionView deselectItemAtIndexPath:selectedRow animated:NO];
    • 我继续编辑答案以包含它。我完全错过了!谢谢!
    • +1 但是,这个答案谈到了 UITableView。尽管对于那些熟悉 Cocoa 的人来说它仍然是一个有用的答案,但如果它提到 UICollectionView 会更好,因为该问题专门询问 UICollectionView。
    【解决方案2】:

    在 Swift 2.0 中:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.collectionView.allowsMultipleSelection = true
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    
        let selectedRows: [NSIndexPath] = self.collectionView.indexPathsForSelectedItems()!
        for selectedRow: NSIndexPath in selectedRows {
            if (selectedRow.section == indexPath.section) && (selectedRow.row != indexPath.row) {
                self.collectionView.deselectItemAtIndexPath(selectedRow, animated: false)
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      在 Swift 5.0 中:

      override func viewDidLoad() {
          super.viewDidLoad()
          self.collectionView.allowsMultipleSelection = true
      }
      
      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
          (collectionView.indexPathsForSelectedItems ?? [])
              .filter { $0.section == indexPath.section && $0.item != indexPath.item && $0.row != indexPath.row }
              .forEach { self.collectionView.deselectItem(at: $0, animated: false) }
      }
      

      如果您希望它在部分中的项目之间切换,请使用:

      override func viewDidLoad() {
          super.viewDidLoad()
          self.collectionView.allowsMultipleSelection = true
      }
      
      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
          (collectionView.indexPathsForSelectedItems ?? [])
              .filter { $0.section == indexPath.section && $0.item }
              .forEach { self.collectionView.deselectItem(at: $0, animated: false) }
      }
      

      我认为这会带来更好的用户体验,但这是你的决定

      【讨论】:

        【解决方案4】:

        您可能需要在-shouldSelect-shouldDeselect 中执行逻辑。也许保留每个部分选择的单元格数量的字典

        NSMutableDictionary *dict;
        
        - (BOOL)collectionView:(UICollectionView *)view shouldSelectItemAtIndexPath:(NSIndexPath *)path
        {
            NSInteger *section = path.section;
            if (!dict)
            {
                dict = [[NSMutableDictionary alloc] init];
            }
            NSNumber *numberSelected = [dict objectForKey:[NSNumber numberWithInteger:section]];
            if (numberSelected > 0)
            {
                return NO;
            }
            else
            {
                [dict setObject:[NSNumber numberWithDouble:numberSelected.doubleValue - 1] forKey:[NSNumber numberWithInteger:section]];
                return YES;
            }
        }
        - (BOOL)collectionView:(UICollectionView *)view shouldDeselectItemAtIndexPath:(NSIndexPath *)path
        {
            NSInteger *section = path.section;
            if (!dict)
            {
                dict = [[NSMutableDictionary alloc] init];
            }
            NSNumber *numberSelected = [dict objectForKey:[NSNumber numberWithInteger:section]];
        
            numberSelected = [NSNumber numberWithDouble:numberSelected.doubleValue - 1];
        
            [dict setObject:numberSelected forKey:[NSNumber numberWithInteger:section]];
        
            return YES;
        }
        

        应该可以的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多