1

UIPickerにかなり複雑な行を作成したいと思います。私が見たすべての例は、そのようにゼロからビューを作成します...

- (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;
}

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

4

2 に答える 2

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
    }
于 2009-09-14T23:53:40.967 に答える
1

最初のアイテムとして .xib リソース内にセルを作成し、次のように参照したいと思います。

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

これにより、セル リソースがテーブル コントローラー(cellFactory Outlet) の知識を必要とするという依存関係がなくなり、セルを複数のコントローラーで簡単に再利用できるようになります。

于 2012-01-24T07:43:41.440 に答える