I am creating multiple labels within a loop, and im confused on memory management since every instance of the label has the same name since they are all created within a loop so would that mean that each time a new label is created within the loop, the last label allocated would be overridden and no longer take up memory or is it still stored in memory. Quite confused, please help, thanks in advance.
- (void)fetchEntrys
{
JournalrAppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:@"Entrys"
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSManagedObject *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:request
error:&error];
if ([objects count] == 0) {
NSLog(@"No matches");
} else {
NSLog(@"Matches found");
matches = objects[1];
NSString *firstEntry = [matches valueForKey:@"entry"];
NSLog(@"First Entry: %@", firstEntry);
self.totlal = [objects count];
int i = [objects count] - 1;
NSLog(@"%i", i);
while ( i > -1) {
matches = objects[i];
NSString *entry = [matches valueForKey:@"entry"];
NSDate *date = [matches valueForKey:@"date"];
NSDateFormatter *formatDate = [[NSDateFormatter alloc]init];
[formatDate setDateFormat:@"MM/dd/YY"];
NSString *dateString = [formatDate stringFromDate:date];
NSLog(@"Date: %@", dateString);
NSLog(@"Entry: %@", entry);
UILabel *dateLabel = [[UILabel alloc] init];
dateLabel.text = dateString;
dateLabel.frame = CGRectMake(20, cY, 100, 30);
dateLabel.numberOfLines = 3;
dateLabel.font = [UIFont boldSystemFontOfSize:24.0];
[dateLabel setTag:i];
UIButton *deleteButton = [[UIButton alloc]initWithFrame:CGRectMake(230, dateLabel.frame.origin.y, 70, 27)];
[deleteButton addTarget:self action:@selector(deleteButtonPressed) forControlEvents:UIControlEventTouchDown];
[deleteButton setBackgroundImage:[UIImage imageNamed:@"delete.jpg"] forState:UIControlStateNormal];
[deleteButton setTag:i];
cY += 35;
UILabel *labelEntry = [[UILabel alloc]init];
labelEntry.numberOfLines = 0;
labelEntry.text = entry;
CGRect lblFrame = CGRectMake(20, cY, 280, 1000);
labelEntry.frame = lblFrame;
labelEntry.textAlignment = NSTextAlignmentLeft;
labelEntry.backgroundColor = [UIColor clearColor];
[labelEntry sizeToFit];
[labelEntry setTag:i];
UILabel *outerLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, cY - 50, 300, labelEntry.frame.size.height + 80)];
[scrollView addSubview:outerLabel];
outerLabel.layer.borderColor = [UIColor grayColor].CGColor;
outerLabel.layer.borderWidth = 1.0;
outerLabel.tag = i;
if(i ==[objects count] - 1){
self.firstY = outerLabel.frame.origin.y;
}
//UIImageView *backgroundImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, cY, 312, labelEntry.frame.size.height)];
//backgroundImage.contentMode = UIViewContentModeScaleToFill;
//backgroundImage.image = [UIImage imageNamed:@"post.jpg"];
//NSLog(@"Image Height :%f", backgroundImage.frame.size.height);
//[scrollView addSubview:backgroundImage];
cY += labelEntry.frame.size.height;
NSLog(@"Label Height: %f", labelEntry.frame.size.height);
NSLog(@"currenty: %f", cY);
cY += 100.f;
scrollView.scrollEnabled = YES;
scrollView.showsVerticalScrollIndicator = YES;
[scrollView addSubview: labelEntry];
[scrollView addSubview:dateLabel];
[scrollView addSubview:deleteButton];
NSLog(@"%i", i);
i--;
}
scrollView.contentSize = CGSizeMake(320, cY);
NSLog(@"cY: %f", cY);
self.currentI = i;