I’m working on a tabbed app that (among other things) creates a simple Facebook feed. The Facebook section is a UINavigationController holding a Master Table View and a Detail View. When I click one of the rows in the Master Table View, it brings up the detail view (and the appropriately filled labels) as intended. However, if I go back to the Master View and select another row, something strange happens. If the “detailLab” label (which is sizeToFit‘d) was, say, four lines long on the first occurrence of Detail View, and the “detailLab” label was 2 lines long on the second occurrence of Detail View, then on the second occurrence of Detail View, “detailLab” will display the first two lines properly, but underneath them will be the last two lines from the first occurrence of Detail View.
And some (hopefully) relevant code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CGRect labelFrame = CGRectMake(100, 40, 220, 150);
detailLab = [[UILabel alloc] initWithFrame:labelFrame];
[detailLab setTextColor:[UIColor darkGrayColor]];
detailLab.font = [UIFont systemFontOfSize:14];
[detailLab setText:detail];
[detailLab setNumberOfLines:0];
[detailLab sizeToFit];
[self.view addSubview:detailLab];
NSURL *url = [NSURL URLWithString:pictureString];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:url];
picture = [UIImage imageWithData:imageData];
bigImage.image = picture;
NSLog(@"dateString: %@", dateString);
dateLabel.text = dateString;
}
- (void)viewDidUnload
{
[super viewDidUnload];
bigImage = nil;
detailLab = nil;
detailLab.hidden = YES;
// Release any retained subviews of the main view.
self.detailDescriptionLabel = nil;
}
If you need any other code or details, just ask; I’d be happy to provide it.
The problem is that each time
viewWillAppearis called you are creating a new label without, I assume, removing it inviewWillDisappear. So the labels are stacking up, one on top of the other.Remember you may get called like this (ignoring irrelevant calls):
A neater solution would be to add your label view in
viewDidLoadand only alter the position of that already existing view inviewWillAppear. You still need to remember to dispose of the view inviewDidUnLoad.Here’s what I mean (note this was only eyeball compiled so you may need to correct some typos):