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

这基本上有效,它在我的 Picker 中显示了一个紫色矩形。

但我希望能够像这样从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 回答