Ios 由于外部数据源和委托,UITableView未正确加载?
我有一个Ios 由于外部数据源和委托,UITableView未正确加载?,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,我有一个UITableView。今天,我将它的UITableViewDataSource和UITableViewDelegate移动到一个单独的文件中。看起来像这样 @interface TableDelegate : NSObject <UITableViewDelegate> @property (nonatomic, assign) id delegate; @end @interface TableDataSource : NSObject <UITableVie
UITableView
。今天,我将它的UITableViewDataSource
和UITableViewDelegate
移动到一个单独的文件中。看起来像这样
@interface TableDelegate : NSObject <UITableViewDelegate>
@property (nonatomic, assign) id delegate;
@end
@interface TableDataSource : NSObject <UITableViewDataSource>
@property (nonatomic, assign) id delegate;
@end
@implementation TableDelegate
@synthesize delegate;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
NSLog(@"heightForHeaderInSection");
return 20.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSLog(@"header!");
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 20)];
headerView.backgroundColor = [UIColor whiteColor];
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"heightForRowAtIndexPath");
return 48.0f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[((UIViewController *)delegate).view endEditing:YES];
}
@end
@implementation RequestTableDataSource
@synthesize delegate;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 6;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"numberOfRowsInSection");
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
NSLog(@"cellForRowAtIndexPath");
cell.textLabel.text = @"Test";
return cell;
}
@end
现在,当我编译我的应用程序时,我得到一个空白的
UITableView
,没有任何内容。我在控制台中看到了一些日志,但没有看到cellforrowatinexpath
和viewForHeaderInSection
的日志。我在这里做错了什么?UITableView
持有对其委托和数据源的弱
引用,因此您正在创建的委托和数据源没有任何对它们的强
引用,并且在您的方法末尾被释放
您应该将它们存储在视图控制器上的
strong
属性中,以便在您使用完它们之前不会释放它们。UITableView
持有对其委托和数据源的弱
引用,因此,您正在创建的对象没有任何内容包含对它们的strong
引用,并且在您的方法结束时被解除分配
您应该将它们存储在视图控制器上的
strong
属性中,以便在您使用完它们之前不会释放它们。UITableViewDelegate充当与表视图交互的助手,因此移动UITableViewDelegate对于代码重用不是一个好主意。但UITableViewDataSource有助于为表视图提供数据。根据标题为(第232课时)的苹果WWDC。它实际上促进了数据源的可重用性,并明确了控制器的责任。您可以将数据源保留为单独的文件。请参阅以作为实现它的参考。UITableViewDelegate充当与表视图交互的助手,因此移动UITableViewDelegate对于代码重用不是一个好主意。但UITableViewDataSource有助于为表视图提供数据。根据标题为(第232课时)的苹果WWDC。它实际上促进了数据源的可重用性,并明确了控制器的责任。您可以将数据源保留为单独的文件。请参阅以作为实现它的参考。是否有任何对象具有对TableDataSource
和TableDelegate
的强引用?是否有任何对象具有对TableDataSource
和TableDelegate
的强引用?
TableDelegate *tableDelegate = [[TableDelegate alloc] init];
tableDelegate.delegate = self;
TableDataSource *tableDataSource = [[TableDataSource alloc] init];
tableDataSource.delegate = self;
self.testTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.testTable.delegate = tableDelegate;
self.testTable.dataSource = tableDataSource;
[self.view addSubview:self.testTable];