我有一个实现 UITableViewDelegate 协议(protocol)的类,还有另一个处理数据的类,即它实现了 UITableViewDataSource 协议(protocol)。
@interface TableViewClass : UITableViewController <UITableViewDelegate>
@interface TableDataSource : NSObject <UITableViewDataSource>
我将 TableViewClass 设置为委托(delegate),将 TableDataSource 设置为数据源
id datasource = [[TableDataSource alloc] init]
[self.tableView setDelegate:self];
[self.tableView setDataSource:dataSource];
我在 TableDataSource 类的 init 方法中从异步服务器调用中获取数据,该方法填充数组并确定表中的行/节数。
但在调用返回一些结果之前,numberOfSectionsInTableView
和 numberOfRowsInSection
被执行,导致 0 行和 0 个部分,因此是一个空表。
我想将 [self.tableView reloadData]
放在 callBack 中,但我无权访问数据源类中的 tableView。
谁能指导我如何通过数据源类在 tableView 中重新加载数据,因为我稍后可能也需要它来刷新数据。
谢谢
最佳答案
为了快速解决这个问题,您可以在 TableDataSource 上为 UITableView 添加一个@property。
// TableDataSource.h
@interface TableDataSource : NSObject <UITableViewDataSource>{
UITableView *tableView;
}
@property(retain)UITableView *tableView;
@end
// TableDataSource.m
@implementation TableDataSource
@synthesize tableView;
- (void)dealloc{
self.tableView = nil;
}
@end
现在,在创建数据源时设置属性:
// UITableViewController.m
TableDataSource* datasource = [[TableDataSource alloc] init]
[datasource setTableView:self.tableView]
[self.tableView setDelegate:self];
[self.tableView setDataSource:dataSource];
当您需要从 DataSource 重新加载数据时,您现在可以:
[self.tableView reloadData];
关于iphone - 如何在实现 UITableViewDataSource 协议(protocol)的类中重新加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5071972/