user4992124 user4992124 - 27 days ago 11
iOS Question

UITableView not loading properly because of external DataSource and Delegate?

I have a

UITableView
. Today, I moved it's
UITableViewDataSource
and
UITableViewDelegate
to a separate file. It looks like this.

@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


I then construct my
UITableView
as below:

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];


Now, when I compile my app I get a blank
UITableView
without any content. I see some of the logs in the console, but not the logs for
cellForRowAtIndexPath
and
viewForHeaderInSection
. What am I doing wrong here?

dan dan
Answer

UITableView holds a weak reference to its delegate and datasource, so the ones you are creating have nothing holding a strong reference to them and are deallocated at the end of your method.

You should store them in a strong property on your view controller so they don't get deallocated until you are done using them.

Comments