2014-10-08 3 views
1

Picker가 포함 된 사용자 지정 UITableViewCell 클래스가 있습니다. 셀과 그 내용은 핵심 데이터 엔티티로 채워집니다. 이 사용자 정의 셀은 둘 이상의보기 컨트롤러에서 볼 수 있으므로 구조, 데이터 소스 및 위임 메소드를 처리해야하는 유틸리티 클래스가 있습니다.UIVIewController, 사용자 지정 UITableViewCell 및 유틸리티 클래스 간의 UIPickerView 데이터 소스 관리

셀프 데이터 소스를 설정할 때 셀이 제대로 표시 될 때처럼 뭔가 빠져있는 것처럼 보입니다. Utility 클래스로 설정하면 numberOfComponentsInPickerView: 메서드 만 호출되고 (lldb)를 제외한 로그에는 아무 것도 나타나지 않으면 응용 프로그램이 충돌합니다. 내 코드 샘플에서는 하나의 구성 요소와 제목이 "Test"인 간단한 행을 가진 간단한 선택 도구를 만들 것입니다.

ViewController.m 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    PickerCell *pCell = nil; 
    Picker *picker = [self.array objectAtIndex:indexPath.row]; 
    UtilityCustomCells *cellUtility = [[UtilityCustomCells alloc]init]; 
    pCell = [cellUtility createPickerCell:tableView withReuseIdentifier:kPickerCellRID andPicker:picker]; 
    pCell.picker.delegate = cellUtility; //does not work 
    pCell.picker.dataSource = cellUtility; //does not work 
    /* This works 
    pCell.picker.delegate = self; 
    pCell.picker.dataSource = self; 
    */ 
    return pCell; 
    } 
} 

마지막

utilityCustomCells.h 

#import "PickerCell.h" 
#import "Picker.h" 

@protocol CustomCellUtilityDelegate <NSObject,UIPickerViewDataSource,UIPickerViewDelegate> 
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; 
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; 
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 

@end 

에서

:

UtilityCustomCells.m 

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    NSLog(@"Here1"); 
    return 1; 
} 

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    NSLog(@"Here2"); 
    return 1; 
} 

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    NSLog(@"Here3"); 
    NSString *title = @"Test"; 
    return title; 
} 

만에 다시 반복 처리는, 단지 "Here2는"로그에 인쇄됩니다. 모든 조언을 많이 주시면 감사하겠습니다. 미리 감사드립니다!

답변

0

나는 이것을 알아낼 수 있었다. cellForRowAtIndexPath 데이터 소스 메서드 내에서 UtilityCustomCell 클래스를 ViewController.m에 인스턴스화했습니다. 사실, 인스턴스 변수를 ViewController.m으로 만들고 alloc/init을 viewDidLoad으로 만들 필요가있었습니다. 그래서 지금 :

ViewController.m 
@interface AssetFormVC() 
{ 
    UtilityCustomCells *cellUtility; 
} 

@end 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    cellUtility = [[UtilityCustomCells alloc]init]; 
    cellUtility.pickerDataSource = [self configurePickerData]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    PickerCell *pCell = nil; 
    Picker *picker = [self.array objectAtIndex:indexPath.row]; 
    pCell = [cellUtility createPickerCell:tableView withReuseIdentifier:kPickerCellRID andPicker:picker]; 
    pCell.picker.delegate = cellUtility; 
    pCell.picker.dataSource = cellUtility; 
    return pCell; 
    } 
} 
지금 UtilityCustomCells.h

@property (nonatomic,strong) NSDictionary *pickerDataSource;

에 속성을 통해 cellUtility에 피커 데이터 소스로 사전을 보낼 수 있습니다