2009-08-24 7 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; 
} 

これももうピッカーが表示されない空白の画面を、生み出します。私はそれを最初の方法で行い、コードで私のサブビューを構築することができますが、ここでは明らかに私が理解していないことがあります。誰か知っていますか?

答えて

2

私はむしろ最初の項目として.xibリソース内のセルを作成し、そのようにそれを参照してくださいだろう、それ自身のペン先に

@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

をセルに入れて:

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

このセルリソースにテーブルコントローラ(cellFactoryアウトレット)の知識が必要な依存関係を取り除き、複数のコントローラでセルを再利用しやすくします。

関連する問題