【问题标题】:Two different UICollectionViewCells, switch between them in a UISegmentedController两个不同的 UICollectionViewCells,在一个 UISegmentedController 中切换它们
【发布时间】:2014-10-18 09:25:01
【问题描述】:

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

【问题讨论】:

    标签: ios xcode uicollectionview uisegmentedcontrol uicollectionviewcell


    【解决方案1】:

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

    首先,在您的故事板中,将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;
    }
    

    【讨论】:

    • 不能让它工作,你想举个例子吗?我已经为此苦苦挣扎了好几天,无法弄清楚!我有一个自定义的分段控制器,所以不知道是不是因为那个..
    • @MartinBorstrand 我创建了一个示例项目:github.com/nikitashytyk/TestSegment
    • 非常感谢!现在将查找并尝试一下!稍后会重新提交!再次感谢!
    • @MartinBorstrand 此代码/示例是否帮助您获得所需的结果?
    • 不像我想的那样,但我可以将它用于其他目的!但是我这样做是为了在同一个控制器中创建两个集合视图,然后将第二个设置为隐藏而第一个显示。它正在工作,但不知道这是一个好方法..感谢您的帮助! @NikitaShytyk
    猜你喜欢
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 2014-10-07
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多