2

I've a view with few UITableView/s and UILabel/s. I'm creating them programmatically (i.e not using NIB).

I've consolidated tableView and label creation in a method with signature:

- (void) CreateTableView:(UITableView*) outTableView andLabel:(UILabel*)OutLabel AtFrame:(CGRect) frame{
CGRect labelFrame = frame;
labelFrame.origin.x = LABEL_LEFT_ALIGNMENT;
labelFrame.origin.y -= LABEL_TOP_ALIGNMENT;
labelFrame.size.height = LABEL_HEIGHT;
outLabel = [[UILabel alloc] initWithFrame:labelFrame];
[[self view] addSubview:outLabel];

outTableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
[outTableView setDataSource:self];
[outTableView setDelegate:self];
[[self view] addSubview:outTableView];
}

Here, outTableView and outLabel are output parameters. That is, after completion of method , caller will be using outTableView and outLabel.

My app has 3 tableview instance variables -- tableView1, tableView2, tableView3. And three label instance variables. View controller (caller) calls like:

   [self CreateTableView:tableView1 andLabel:label1 AtFrame:frame1];
   [self CreateTableView:tableView2 andLabel:label2 AtFrame:frame2];
   [self CreateTableView:tableView3 andLabel:label3 AtFrame:frame3];

After completion of this method, UILabel* are rendered on screen, caller able to use UILabel* object. Strangely, that is not the case with UITableView* object.

Any idea, why there is different behaviour?

Note: My application is ARC enabled.

4

1 回答 1

2

错误的。参数实际上并不包含分配/初始化的实例,因为您是通过值而不是通过引用传递它们。考虑传递一个指向对象的指针,然后在分配新实例时取消引用它:

- (void)createTableView:(UITableView **)tvPtr
{
    *tvPtr = [[UITableView alloc] init...];
    // etc.
}

像这样调用:

UITableView *tv;
[self createTableView:&tv];
于 2012-04-19T15:37:56.757 回答