分段控制容器视图和 UITableViews

Segmented Control Container View and UITableViews

我有一个视图控制器,导航栏中有一个分段控件,分为 3 个部分。所有三个部分都将显示完全相同类型的单元格,但数据会发生变化。使用 3 个容器和 hide/show 它们来匹配屏幕上应该显示的数据是否更有效,还是应该用不同的数据重新加载 table?

重新加载 table 不同的数据会更有效率。

您可以为每个委托方法做类似的事情:

Swift version

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if segmentedControl.selectedSegmentIndex == 0 {
        return favoris.count
    } else if segmentedControl.selectedSegmentIndex == 1 {
        return somethingElse.count
    } else {
        return somethingElse.count
    }
}

Objective C version

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (segmentedControl.selectedSegmentIndex == 0)
        return [favoris count];
    else if (segmentedControl.selectedSegmentIndex == 1)
        return [somethingElse count];
    else 
        return [somethingElse count];
}

另一种方法是只更改有关 segmentedControl 索引的源数组,这将避免所有 if 语句。

我在一些项目中使用了这种功能,我可以确保性能良好。

希望对您有所帮助!