2016-11-05 5 views
1

私はUITableViewを持っています。今日、私はそれが別のファイルにUITableViewDataSourceUITableViewDelegateだ移動しました。それはこのように見えます。外部データソースとデリゲートのためにUITableViewが正しく読み込まれませんか?

@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次のように:私は私のアプリをコンパイルするとき

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

は今、私は任意のコンテンツずに空白UITableViewを取得します。私はcellForRowAtIndexPathviewForHeaderInSectionのコンソールでログの一部ではなく、ログを参照してください。私はここで間違って何をしていますか?

+0

'TableDataSource'と' TableDelegate'への強い参照を保持している任意のオブジェクトはありますか? – crizzis

答えて

0

UITableViewは、その代理人とデータソースへの参照を保持しているため、作成中のものは何も持たずに、strongの参照を保持し、メソッドの最後に割り当てが解除されます。あなたがそれらを使用して行われるまで、彼らが割当て解除されませんので、

あなたは彼らがあなたのビューコントローラ上のstrongプロパティに格納する必要があります。

0

UITableViewDelegateは、テーブルビューと対話するためのヘルパーとして働き、そこで、移動UITableViewDelegateは、コードの再利用のための良いアイデアではありません。しかし、UITableViewDataSourceはテーブルビューのデータを提供するのに役立ちます。リンゴのWWDCはAdvanced User Interfaces With Collection Views(セッション232)と題されています。実際には、データソースの再利用性を促進し、コントローラの責任を明確にします。データソースを別のファイルにしておくことができます。それを実装するための参照としてgithub repo KPPopOverTableViewを参照してください。

関連する問題