addSubview对于引用计数操作


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdetifier = @"blogCellIdentifier";
UITableViewCell *cell = [m_tableViewDownList dequeueReusableCellWithIdentifier:cellIdetifier];
if(cell==nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetifier] autorelease];
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"asd"]];
[cell setBackgroundView:image];
[image release];
UIImageView *imgLeft = [[UIImageView alloc] initWithFrame:CGRect_Cell_LeftImg];
imgLeft.tag = 201;
[color=red] //这里addsubview为imgLeft引用计数+1,此时imgLeft为2[/color]
[cell addSubview:imgLeft];
[imgLeft release];

NSString *path1 = [[NSBundle mainBundle] pathForResource:kTableViewCellRightImg ofType:@"png"];
UIImageView *imgRight = [[UIImageView alloc] initWithFrame:CGRect_Cell_Rightimg];
imgRight.image = [UIImage imageWithContentsOfFile:path1];
imgRight.tag = 202;
[cell addSubview:imgRight];
[imgRight release];

}
NSString *path = [[NSBundle mainBundle] pathForResource:[m_downListDataSource objectAtIndex:indexPath.row] ofType:@"png"];
UIImageView * vv = (UIImageView*)[cell viewWithTag:201];
vv.image = [UIImage imageWithContentsOfFile:path];
return cell;
}


[color=darkred]在我们调用这个函数(比如是[self.view addSubview:tableview])之后,tableview是否应该release?
这里要分情况,如果tableview是一个类的属性,在类对象释放的时候会自动释放,那么就不必再次释放。反之,如果他只是一个临时的对象那么就应该在加入之后进行释放。这也是符合上面所说的规则的。

self.view是tableview的管理者,他就应该在加入tableview的时候对他进行retain,在本身释放的时候release。而事实上,ios也正是这么做的。[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值