【问题标题】:Populating Two UITableView in the same View在同一个视图中填充两个 UITableView
【发布时间】:2011-08-10 14:35:50
【问题描述】:

我在同一个视图中有 2 个 UITableView,我的问题是如何在 viewDidLoad 方法中使用不同的数据填充它们?谢谢。

【问题讨论】:

  • 首先,您不会在 viewDidLoad 中填充数据。您使用 tableView 的数据源和委托回调。其次,您的问题已经在您的上一篇文章中得到解答:stackoverflow.com/questions/5747889/…
  • 在我的情况下,我使用 plist 文件和我看到的所有教程,使用这种方法!我如何使用不同的 plist 文件来填充它们?

标签: iphone uitableview datasource viewdidload


【解决方案1】:

回答问题的 plist 部分:

NSString *filePath=[[[NSBundle mainBundle] pathForResource:@"someName" ofType:@"plist"] retain];
NSDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:filePath] retain];

所以只需使用其中的两个,每个表一个,然后只使用两个 tableView,看起来你已经明白了。

【讨论】:

  • 好的,谢谢,但是把这个放在哪里?用什么方法?一般我把它放在viewDidLoad方法中,但似乎其他有经验的程序员说它是错误的!
  • 向属性添加延迟加载方法。我会将其添加到我的答案中。
  • 我不知道为什么那个人反对-viewDidLoad - 我肯定会把它放在viewDidLoad 中,而且我在过去的应用程序中也有,它工作正常。
【解决方案2】:

您将它们填充到

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

在该函数中,您可以比较 tableView 以查看它是什么表,并为该表加载适当的数据。

//Assume the following property
@property (nonatomic, retain) NSDictionary *myData;

将此添加到代码文件中

- (NSDictionary) myData
{
    if (myData == nil) //Notice I am accessing the variable, not the property accessor.
    {
        NSString *filePath=[[NSBundle mainBundle] pathForResource:@"someName" ofType:@"plist"];
        myData = [[NSDictionary dictionaryWithContentsOfFile:filePath] retain];
    }
    return myData;
}

然后当您访问 self.myData 属性时,如果它尚未打开,它将打开。

【讨论】:

  • 好的,我可以使用 if (tableView==table1)...else,但是我不知道如何使用 plist 文件来填充它们?
  • 将 plist 加载到 NSDictionary 或 NSArray... 您需要确定 NSIndexPath 如何填充单元格。
【解决方案3】:

所有委托和数据源协议方法总是将指针传递给它们被调用的tablesView。只需做一个 if 来检查每种方法的含义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多