2009-08-24 4 views
1

UIPicker에서 상당히 복잡한 행을 만들고 싶습니다. 이 기본적으로 작동 내가 지금처럼 처음부터보기를 만들 본 적이 모든 예제 ...UIPickerView viewForRow에서 NIB에서 행로드

- (UIView *) pickerView:(UIPickerView *)pickerView 
viewForRow:(NSInteger)row 
forComponent: (NSInteger)component reusingView:(UIView *)view 
{ 
    CGRect cellFrame = CGRectMake(0.0, 0.0, 110.0, 32.0); 
    UIView *newView = [[[UIView alloc] initWithFrame:cellFrame] autorelease]; 
    newView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:1.0 alpha:1.0]; 
    return newView; 
} 

는, 내 선택기에서 보라색 사각형을 보여줍니다.

그러나 나는 그렇게 같은 NIB 파일에서 pickerView 항목을로드 할 수 있도록하고 싶습니다 ...

- (UIView *) pickerView:(UIPickerView *)pickerView 
viewForRow:(NSInteger)row 
forComponent: (NSInteger)component reusingView:(UIView *)oldView 
{ 

    NSArray * nibs = [[NSBundle mainBundle] loadNibNamed:@"ExpenseItem" owner:self options:nil]; 
    UIView *newView = [nibs objectAtIndex:0]; 
    return newView; 
} 

이도 더 이상 선택기를 표시하지 않는 빈 화면을 얻을 수 있습니다. 나는 그것을 첫 번째 방법으로 할 수 있으며 코드에서 내 하위 뷰를 만들 수 있습니다. 그러나 분명히 이해할 수없는 일이 여기에 있습니다. 아무도 몰라?

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LapCellID"]; 
if(!cell) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed: cellNibName owner: nil options: nil]; 
    cell = [nib objectAtIndex: 0]; 
} 

이를 :

답변

2

과 같이 그것을 참조 다음

@interface 
    IBOutlet UITableViewCell *cellFactory; 


@implementation 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LapCellID"]; 
    if(nil == cell) { 
     [[NSBundle mainBundle] loadNibNamed:@"LapCell" owner:self options:nil]; 
     cell = [cellFactory retain]; // get the object loadNibNamed has just created into cellFactory 
     cellFactory = nil; // make sure this can't be re-used accidentally 
    } 
1

내가 오히려 첫 번째 항목으로 .xib 자원 내부의 세포를 만들 것 그 자체가 펜촉 셀을 넣고 셀 리소스가 테이블 컨트롤러 (cellFactory 아울렛)에 대한 지식을 필요로하는 종속성을 제거하여 여러 컨트롤러에서 셀을 더 쉽게 재사용 할 수있게합니다.