gpt4 book ai didi

ios - 具有多个数据数组的 UITableView

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:26:03 25 4
gpt4 key购买 nike

我有一个应用程序,其中有 7 个不同的 UITableViewController。所有 7 个都通过 tabBarController 链接。我正在寻找一种在所有 7 个 UITableViewController 中使用单个自定义类的方法。我有 7 个不同的数组,它们都包含特定数量的对象。我需要知道如何:

  • 更改 tableView 中的行数,具体取决于我用作数据源的数组。
  • 根据用户当前正在查看的 ViewController 更改用作数据源的数组(这甚至可以完成吗?)
  • 根据用作数据源的数组更改单元格的内容。

我熟悉将 UITableView 用于单个数据源,但我真的不知道如何使用多个数据源。

谢谢!

最佳答案

您可以让一个类成为所有 UITableViewController 的数据源

您可以通过创建 UITabBarController 的自定义子类来实现这一点,该子类保留一个 UITableViewController 数组和一个对应的字典,该字典将 UITableVC 映射到它的数据源使用的数组。

将其设置为所有 UITableView 的数据源,然后像我下面的示例一样处理每个数据源方法。

看看 UITableViewDataSource docs .所有的方法都在他们试图获取信息的 tableView 中传递。

例如:

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Here you could compare the tableView var to your array of tableViews to figure out which table view called this
//Based on that you could query your dictionary to find the array that houses the data for that tableView.
//Use the indexPath to find the data that you need to create and return the right cell
}

关于ios - 具有多个数据数组的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30606842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com