代码之家  ›  专栏  ›  技术社区  ›  Ben S

是否可以通过编程设置UITableViewController的数据源?

  •  2
  • Ben S  · 技术社区  · 15 年前

    我使用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];
    }
    

    但是表保持为空,永远不会调用数据源方法( numberOfRowsInSection cellForRowAtIndexPath )

    编辑: 我的 sharedInstance 是四轮马车,零分还回来。从未调用过任何方法。

    4 回复  |  直到 15 年前
        1
  •  0
  •   jbrennan    15 年前

    尝试

    [self.tableView setDataSource:myDataSource];
    

    从UITableViewController子类(即 self )

        2
  •  0
  •   Kristopher Johnson    15 年前

    设置数据源后,是否调用 reloadData 要使表视图使用新的数据源吗?

        3
  •  0
  •   Lyndsey Ferguson    15 年前

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

        4
  •  0
  •   Ben S    15 年前

    问题是我的 sharedInstance 方法有一个bug,它可以返回nil。因为发送消息给nil被悄悄地忽略了,所以我的课上从来没有要求过任何东西。