【问题标题】:Is it possible to programmatically set a UITableViewController's dataSource?是否可以以编程方式设置 UITableViewController 的数据源?
【发布时间】:2010-12-11 08:35:14
【问题描述】:

我在 Xcode 中使用基于导航的模板创建了一个应用程序。我还创建了一个实现 UITableViewDataSource 协议的类,并希望将它用于 RootViewController 中的 UITableView。

我尝试在 viewDidLoad 和各种初始化程序中设置 dataSource 属性,但我无法让它工作。

我该怎么做?

目前我正在做以下事情:

- (void)viewDidLoad {
    [super viewDidLoad];

    // The Navigation Controller uses this to display the title in the nav bar.
    self.title = @"Names";

    // Little button with the + sign on the right in the nav bar
    self.navigationItem.rightBarButtonItem = 
        [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showAddNameScreen)];

    // Set the data source and reload.
    [self.tableView setDataSource:[DataAcceess sharedInstance]];
    [self.tableView reloadData];
}

但表只是保持为空,并且永远不会调用数据源方法(numberOfRowsInSectioncellForRowAtIndexPath

编辑:我的sharedInstance 有问题并返回零。从未调用过任何方法。

【问题讨论】:

  • 您能否详细说明“无法正常工作”的含义?有编译错误吗?表格视图是否显示为空?它崩溃了吗?
  • 表为空,UITableViewDataSource 方法中的断点没有被调用。

标签: iphone datasource uitableview


【解决方案1】:

试试

[self.tableView setDataSource:myDataSource];

来自 UITableViewController 子类(即self)。

【讨论】:

  • 用什么方法?我已经在 viewDidLoad 中尝试过,但无法让它工作。
【解决方案2】:

设置dataSource后,是否调用reloadData让table view使用新的数据源?

【讨论】:

    【解决方案3】:

    您的表数据源是否返回非零行数?

    【讨论】:

      【解决方案4】:

      问题是我的 sharedInstance 方法有一个错误,它可以返回 nil。由于向 nil 发送消息会被默默忽略,因此我的课堂上没有任何调用过。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多