2013-04-01 5 views
1

"MasterViewController"라고하는 UITableViewController 클래스가 있습니다. MasterViewController 내에서 다른 UITableViewController (MasterViewController 아님)를 사용하는 tableview를 표시하려고합니다. 다른 tableview 이미 해당 대리자 및 데이터 원본으로 MasterViewController 사용하여이 일을하고있다.UITableViewController 대리자

나는 MasterViewController의 메소드에서 다음과 같은 논리를가집니다.

ToTableViewController *toController = [[ToTableViewController alloc] init]; 
UIView *toView = [[UIView alloc] initWithFrame:CGRectMake(10,10,250,200)]; 
//toView.backgroundColor = [UIColor redColor]; 
UITableView *toTableView = [[UITableView alloc] initWithFrame:CGRectMake(10,10,220,180) style:UITableViewStylePlain]; 

[toTableView setDelegate:toController]; 
[toTableView setDataSource:toController]; 

[toView addSubview:toTableView]; 

[self.view addSubview:toView]; 

[toTableView reloadData]; 

은 내가 ToTableViewController이 새로운있는 tableview (toTableView)의 위임 및 데이터 소스가되고 싶어요.

문제는 내 ToTableViewController cellForRowAtIndexPath 메서드가 호출되지 않는 것입니다. 사실 위임 메서드 중 아무 것도 호출되지 않습니다.

의견을 보내 주시면 감사하겠습니다.

답변

0

나는 당신의 문제는 toController 너무 일찍 출시되고있는 것입니다 확신합니다. ARC를 사용하여 (나는 당신도 그렇게 생각한다), 나는 당신이해야 할 일을 따라 따라 놀았다. 그리고 위임 메서드가 호출되지 않는 것처럼 보이는 동일한 결과를 얻었습니다. 해결 된 것은 toController에 지역 변수를 사용하지 않는 것이 었습니다. 대신 다음 코드에서 참조 할 변수 이름 _toController을 사용

@property (strong, nonatomic) ToTableViewController *toController; 

처럼 MasterViewController 클래스 멤버로 선언합니다.

EDIT : ToTableViewController가 UITableViewController에서 상속받은 첫 번째 테스트에서 명확 해졌습니다. 이후로 난 정말 그 필요가 없었어요 추가 및 UITableView 추가 및 너무 (당신은 그냥 _toController.view 직접 사용할 수있는) 대리인을 첨부 할 수 있습니다. 그래서이 두 번째 테스트에서 나는 처음부터 ToTableViewController 대리인 프로토콜에서 상속받은 상속에서 만든 별도의 UITableView가 필요하게됩니다. 그냥 완전성 여기를 작동하는 코드입니다 :

ToTableViewController.h는

#import <Foundation/Foundation.h> 

@interface ToTableViewController : NSObject <UITableViewDelegate, UITableViewDataSource> 

@end 

ToTableViewController.m

#import "ToTableViewController.h" 

@implementation ToTableViewController 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 13; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    NSString *str1 = @"Row #"; 
    cell.textLabel.text = [str1 stringByAppendingFormat:@"%d", indexPath.row+1]; 

    return cell; 
} 

@end 

MasterViewController.m (같이 내가하는 .m 파일에 toController를 선언하지만, .h 파일 대신 .h 파일을 사용할 수 있습니다.)

#import "MasterViewController.h" 
#import "ToTableViewController.h" 

@interface MasterViewController() 

@property (strong, nonatomic) ToTableViewController *toController; 

@end 

@implementation MasterViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    _toController = [[ToTableViewController alloc] init]; 
    UIView *toView = [[UIView alloc] initWithFrame:CGRectMake(10,10,250,200)]; 
    toView.backgroundColor = [UIColor redColor]; 

    UITableView *toTableView = [[UITableView alloc] initWithFrame:CGRectMake(10,10,220,180) style:UITableViewStylePlain]; 

    [toTableView setDelegate:_toController]; 
    [toTableView setDataSource:_toController]; 

    [toView addSubview:toTableView]; 

    [self.view addSubview:toView]; 
} 

@end 
+0

jross, 나를 도와 주셔서 감사합니다. 이것은 작동하는 것 같습니다. 흥미롭게도 필자는 원래 toController를 MasterViewController의 속성으로 선언하고 동일한 문제를 겪고있었습니다. 나는 당신이 제안한대로 그것을 뒤로 옮겼다. 아마 xCode의 마지막 세션에서 뭔가가 손상되었을 수도 있습니다. 어쨌든, 이것은 내 문제를 해결했습니다. 매우 감사합니다. Tim –

+0

@TDavey, 비슷한 유형의 시나리오를 실험 해본 이후로 당신의 질문에 흥미가있었습니다. – jross

관련 문제