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


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

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

猜你喜欢

转载自zcw-java.iteye.com/blog/1845544
今日推荐