【问题标题】:Load three UITableViews from different datasources从不同的数据源加载三个 UITableView
【发布时间】:2011-05-10 06:38:24
【问题描述】:

我在一个视图上有三个 uiTableView。

我创建了三个加载不同数据的不同 NSMutableArray。

我需要将其中一个 NSMutableArray 作为 UITableView 之一的数据源。

我可以通过表单的 viewDidLoad 分配所有三个 UITableViews 数据源。

但我真正需要做的是将每个 UITableView 数据源分配给不同的 NSMutableArray。

我该如何执行这项任务?

谢谢
托尼

【问题讨论】:

    标签: xcode uitableview nsmutablearray datasource


    【解决方案1】:

    如果所有三个 UITableView 共享同一个数据源对象(包含所有三个数组的对象),那么只需使用 if 语句来区分请求数据的表视图:

    - (NSInteger)tableView:(UITableView *)tableView 
     numberOfRowsInSection:(NSInteger)section
    {
        // If table 1 is asking, give it table 1 data...
        if (tableView == myTableView1)
        {
            // Assume all sections have 3 rows each for
            // purposes of simple demonstration...
            return [dataSourceForTable1 count];
        }
    
        // If table 2 is asking, give it table 2 data...
        if (tableView == myTableView2)
        {
            // Assume all sections have 3 rows each for
            // purposes of simple demonstration...
            return [dataSourceForTable2 count];
        }
    
        // If table 3 is asking, give it table 3 data...
        if (tableView == myTableView3)
        {
            // Assume all sections have 3 rows each for
            // purposes of simple demonstration...
            return [dataSourceForTable3 count];
        }
    
       // The compiler will complain if we don't have
       // a final return since it's possible none of the 
       // if statements will be true ...
       return 0;
    }
    

    【讨论】:

    • 对不起 - 这对我来说没有意义。里面发生了什么......目前我只有返回 [listOfLocations 计数];在方法中。我需要每个 tableview 有一个不同的数据集。
    • 您应该仔细阅读相关文档。表视图可以共享一个数据源。当每个人都要求提供特定信息(例如一节中的行数)时,您的答案取决于要求它的表。我会更新代码来举个例子。
    • - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    猜你喜欢
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    相关资源
    最近更新 更多