UITableView:以编程方式设置数据源
在我的iPhone应用程序中,我有一个UIViewController,它有一个UITableView和另一个UIView,在它的xib文件中,xib的视图包含UITableView和另一个UIView。两个视图都作为 IBOutlet 链接到 xib 文件中的相应视图。 当我想以编程方式为 UITableView 设置数据源时,我在 UIViewController 中创建一个类似这样的方法:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil tableDataSource:(id)dataSource{
if ((self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
self.inventoryTableView.dataSource = dataSource;
//[self.inventoryTableView reloadData];
}
return self;}
然后在我的另一个类中实现以下方法,我们将其称为 B:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
最后我分配 + init B 并传递实例到创建 UIViewController 的方法 initWithNibName:bundle:tableDataSource:
。所有视图都出现在模拟器中,但数据源方法似乎没有被调用。 我对另一个控制器做了类似的事情并且有效,但它是一个 UITableViewController 而 TableView 是控制器的视图。作为子视图,我是否必须使用与 initWithNib 不同的另一种方法设置数据源?
更新: 通过将数据源分配放入 viewDidLoad: 方法中,我能够解决这个问题。在我看来,即使在调用 [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]
之后,主视图的子视图也没有正确初始化
In my iPhone app I have a UIViewController that has a UITableView and another UIView, in its xib file, the view of the xib contains both the UITableView and the other UIView. Both view are linked as IBOutlets to the appropriate views in the xib file.
When I want to set the data source programmatically for the UITableView I create a method like this in the UIViewController:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil tableDataSource:(id)dataSource{
if ((self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
self.inventoryTableView.dataSource = dataSource;
//[self.inventoryTableView reloadData];
}
return self;}
and then I implement the following methods in another class of mine, let's call it B:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Finally I alloc+init B and pass the instance to the method initWithNibName:bundle:tableDataSource:
creating the UIViewController. All the views appear in the simulator but the data source methods appear not to be called.
I do something similar with another controller and works, but it is a UITableViewController and the the TableView is the controller's view. Is it possible that being a subview i have to set the data source in another method different from the initWithNib?
UPDATE:
By putting the datasource assignation in the viewDidLoad:
method I was able to solve it. It appears to me that even after calling [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]
the subViews of the main view are not properly inited
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这个答案,我认为它与您建议使用“B”作为控制器的外部数据源类似。
将 UITableview 数据源和委托与 main 分离的简单方法UIViewController 类?
Take a look at this answer, I think it's similar to what you're proposing with using "B" as the external datasource for the controller.
Simple way to separate UITableview datasource and delegate from main UIViewController class?
正如更新和评论中所述,我通过更改设置委托/数据源的位置来解决,因为需要等待 tableView 被分配和初始化。在我看来,作为子视图,它的分配/初始化晚于类的主视图
As said in the update and in a comment, I resolved by changing where to set the delegate/data source because it was necessary to wait for the tableView to be allocated and inited. It appears to me that as a subview it gets allocated/inited later than the main view of the class