ios - 分段控制容器 View 和 UITableViews

标签 ios xcode swift uitableview uicontainerview

我有一个 View Controller ,导航栏中有一个分段控件,分为 3 个部分。所有三个部分都将显示完全相同类型的单元格,但数据会发生变化。拥有 3 个容器并隐藏/显示它们以匹配屏幕上应显示的数据,还是应该使用不同的数据重新加载表格更有效?

最佳答案

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

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

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 语句。

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

希望对您有所帮助!

关于ios - 分段控制容器 View 和 UITableViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38040366/

相关文章:

ios - 无法从 AFNetworking 的成功 block 调用自定义委托(delegate)

ios - 如何调整 UITextView 的大小,使其根据内容自动调整其宽度和高度,并在达到特定值时固定宽度?

xcode - xcuserdata 文件夹中所有内容的 .gitignore 不会忽略 xcuserstate 文件

iphone - 如何以编程方式向下滚动 UITable

即使在更改后,iOS 应用程序仍显示旧应用程序图标

Swift:表达式类型在没有更多上下文的情况下不明确

swift - CollectionViewcell自动布局高度、动态高度、内容高度

ios - 将 Json 与 post 请求绑定(bind)

iphone - 为什么我无法在 Xcode 4.2 中使用 iPhone OS 3.0 作为部署目标进行编译?

ios - 如何在应用程序关闭时执行操作?