tags:

views:

45

answers:

1

I have a separate method to create customized UITableViewCell, Which is given Below

-(UITableViewCell*)getCellContentView:(NSString*)cellIdentifier
{
CGRect photoFrame=CGRectMake(10, 10, 60, 60);
CGRect label1Frame=CGRectMake(85, 9, 200, 32);
CGRect label2Frame=CGRectMake(85, 38, 200, 25);
CGRect labelBgFrame=CGRectMake(0, 0, 320, 80);

UITableViewCell *cell=[[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 300, 80) reuseIdentifier:cellIdentifier] autorelease];


UILabel *tmp;
tmp=[[UILabel alloc] initWithFrame:label1Frame];
tmp.tag=1;
tmp.textColor=[UIColor blackColor];
tmp.font=[UIFont boldSystemFontOfSize:18];
tmp.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:tmp];
[tmp release];
}

Here, @tmp@ is already released ---" Then how, iPhone remembers released object & we can set text to that label.

+2  A: 

I think I understand your question: you're asking how cell.contentView maintains its content after you call [tmp release], is that right?

If so, the answer is that addSubview increments the reference count (like calling retain). So even after you call [tmp release], tmp still has a reference count of one.

Its been a while since I touched the iPhone sdk, but I think that's correct. Corrections welcome.

Alex
You're right, Alex. The tmp object is not yet released, because cell.contentView has a retained pointer to it. sagar, if you want to see the retain count in action, you can add the line NSLog(@"retainCount is now %d", [tmp retainCount]); in several places in your code. You'll see it goes from 1 up to 2 and back down to 1 at the very end -- never 0, which would actually release it.
Tyler
Tyler, thanks for the feedback.
Alex