代码之家  ›  专栏  ›  技术社区  ›  Martin Borstrand

两个不同的UICollectionViewCells,在UISegmentedController中切换

  •  0
  • Martin Borstrand  · 技术社区  · 10 年前

    我想知道在同一个UICollectionView中使用两个不同的UICollectionViewCells标识符最简单的方法是什么?我有一个UISegmentedController,我想在不同风格的UICollectionViewCells之间切换。。在哪里实现哪个代码。我有一个PatternViewCell用于第一个Cell,但如何使用另一个?请指教!

    enter image description here

    1 回复  |  直到 9 年前
        1
  •  0
  •   Nikita Shytyk    10 年前

    对于具有单个数据源的单个集合视图,可以为单个单元格类注册两个集合视图单元格原型。

    首先,在故事板中,设置 Cell1 作为第一小区原型的重用标识符,以及 Cell2 第二个。他们俩都应该有 PatternViewCell

    然后,在更改分段控件的值时,重新加载集合视图:

    - (IBAction)segmentedControlValueChanged:(id)sender {
        [self.collectionView reloadData];
    }
    

    将此操作绑定到 Value Changed 事件

    然后在 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 方法,您可以根据所选索引选择重用标识符。

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
        NSString *identifier = nil;
        if (self.segmentedControl.selectedSegmentIndex == 0) {
            identifier = @"Cell1";
        } else {
            identifier = @"Cell2";
        }
    
        PatternViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        //configure cell
        return cell;
    }