I modified your code a bit and added a comment. Pay attention to the highlighted part.
-(void)setUpHeaders:(NSInteger)fortype{ headers = [NSMutableArray array]; if(fortype == GROUP_BY_TIME){ for(int i=0;i<4;i++){ UIView* header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 68)]; header.backgroundColor = [UIColor whiteColor]; [headers addObject:header]; } int today = (int)[NSDate dayOfTheWeekOfDate:[NSDate date]]; int firstDayOfTheWeek = (int)[defaults integerForKey:FIRST_DAY_IF_THE_WEEK_KEY]; NSString* restOfTheWeek = nil; if(today == ((firstDayOfTheWeek + 4) % 7) + 1) restOfTheWeek = MyLocalizedString(@"THE COMING WEEK", @"THE COMING WEEK") ; else if(today == ((firstDayOfTheWeek + 5) % 7) + 1) restOfTheWeek = MyLocalizedString(@"REST OF NEXT WEEK", @"REST OF NEXT WEEK") ; else restOfTheWeek = MyLocalizedString(@"REST OF THE WEEK", @"REST OF THE WEEK") ; UILabel* lbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 38, 300, 30)]; lbl.tag = 22; lbl.font = XX_LARGE_VOLKS_FONT; [lbl setBackgroundColor:[UIColor clearColor]]; [lbl setTextColor:[UIColor darkGrayColor]]; [lbl setText:MyLocalizedString(@"TODAY", @"TODAY") ];
[[headers objectAtIndex: 0] addSubview: lbl]; // pay attention to this part [lbl sizeToFit]; // always add a view to its parent object before calling 'sizeToFit' or it will crash
lbl = [[UILabel alloc] initWithFrame:CGRectMake([MyLocalizedString(@"TODAY", @"TODAY") sizeWithFont:[UIFont fontWithName:@"Volkswagen-Serial-Medium-Regular" size:21]].width + 15, 41, 300, 30)]; lbl.tag = 2; lbl.font = [UIFont fontWithName:@"Volkswagen-Regular" size:19]; [lbl setBackgroundColor:[UIColor clearColor]]; [lbl setTextColor:[UIColor darkGrayColor]]; NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]]; [lbl setText:[NSString stringWithFormat:@" %@, %@ %d ",[[[NSLocale currentLocale] localeIdentifier] containSubString:@"he"] ? [[[NSDate date] dayOfTheWeekString] uppercaseString] : [[[NSDate date] threeLetterDayOfTheWeekString] uppercaseString],[NSDate abbriviatedMonthForDate:[NSDate date]],(int)[components day]]]; lbl.textAlignment = NSTextAlignmentCenter;
[[headers objectAtIndex: 0] addSubview: lbl]; // pay attention to this part [lbl sizeToFit]; // always add a view to its parent object before calling 'sizeToFit' or it will crash
UIImageView *img; img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 67, 320, 1)]; [img setBackgroundColor:[UIColor darkGrayColor]]; img.alpha = 0.5; img.tag = 3; [[headers objectAtIndex:0] addSubview:img]; img = [[UIImageView alloc] initWithFrame:CGRectMake(lbl.frame.origin.x+2, 40, 1, 19)]; [img setBackgroundColor:[UIColor darkGrayColor]]; img.alpha = 0.5; img.tag = 33; [[headers objectAtIndex:0] addSubview:img]; lbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 38, 300, 30)]; lbl.tag = 22; lbl.font = XX_LARGE_VOLKS_FONT; [lbl setBackgroundColor:[UIColor clearColor]]; [lbl setTextColor:[UIColor darkGrayColor]]; [lbl setText:MyLocalizedString(@"TOMORROW", @"TOMORROW")];
[[headers objectAtIndex: 1] addSubview: lbl]; // pay attention to this part [lbl sizeToFit]; // always add a view to its parent object before calling 'sizeToFit' or it will crash
lbl = [[UILabel alloc] initWithFrame:CGRectMake([MyLocalizedString(@"TOMORROW", @"TOMORROW") sizeWithFont:[UIFont fontWithName:@"Volkswagen-Serial-Medium-Regular" size:21]].width + 15, 41, 200, 30)]; lbl.tag = 2; lbl.font = [UIFont fontWithName:@"Volkswagen-Regular" size:19]; [lbl setBackgroundColor:[UIColor clearColor]]; [lbl setTextColor:[UIColor darkGrayColor]]; components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate dateWithTimeIntervalSinceNow:86400]]; [lbl setText:[NSString stringWithFormat:@" %@, %@ %d ",[[[NSLocale currentLocale] localeIdentifier] containSubString:@"he"] ? [[[[NSDate date] dateByAddingDays:1] dayOfTheWeekString] uppercaseString] : [[[[NSDate date] dateByAddingDays:1] threeLetterDayOfTheWeekString] uppercaseString],[NSDate abbriviatedMonthForDate:[[NSDate date] dateByAddingDays:1]],(int)[components day]]]; lbl.textAlignment = NSTextAlignmentCenter;
[[headers objectAtIndex: 1] addSubview: lbl]; // pay attention to this part [lbl sizeToFit]; // always add a view to its parent object before calling 'sizeToFit' or it will crash
img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 67, 320, 1)]; [img setBackgroundColor:[UIColor darkGrayColor]]; img.alpha = 0.5; img.tag = 3; [[headers objectAtIndex:1] addSubview:img]; img = [[UIImageView alloc] initWithFrame:CGRectMake(lbl.frame.origin.x+2, 40, 1, 19)]; [img setBackgroundColor:[UIColor darkGrayColor]]; img.alpha = 0.5; img.tag = 33; [[headers objectAtIndex:1] addSubview:img]; lbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 38, 300, 30)]; lbl.tag = 2; lbl.font = XX_LARGE_VOLKS_FONT; [lbl setBackgroundColor:[UIColor clearColor]]; [lbl setTextColor:[UIColor darkGrayColor]]; [lbl setText:restOfTheWeek]; [[headers objectAtIndex:2] addSubview:lbl]; img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 67, 320, 1)]; [img setBackgroundColor:[UIColor darkGrayColor]]; img.alpha = 0.5; img.tag = 3; [[headers objectAtIndex:2] addSubview:img]; lbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 38, 300, 30)]; lbl.tag = 2; lbl.font = XX_LARGE_VOLKS_FONT; [lbl setBackgroundColor:[UIColor clearColor]]; [lbl setTextColor:[UIColor darkGrayColor]]; [lbl setText:MyLocalizedString(@"LATER", @"LATER")]; [[headers objectAtIndex:3] addSubview:lbl]; img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 67, 320, 1)]; [img setBackgroundColor:[UIColor darkGrayColor]]; img.alpha = 0.5; img.tag = 3; [[headers objectAtIndex:3] addSubview:img]; lbl = (UILabel*)[[headers objectAtIndex:0] viewWithTag:2]; todayWeather = [[UIImageView alloc] initWithFrame:CGRectMake(lbl.frame.size.width + lbl.frame.origin.x-5, 38, 30, 25)]; [[headers objectAtIndex:0] addSubview:todayWeather]; lbl = (UILabel*)[[headers objectAtIndex:1] viewWithTag:2]; tomorrowWeather = [[UIImageView alloc] initWithFrame:CGRectMake(lbl.frame.size.width + lbl.frame.origin.x-5, 38, 30, 25)]; [[headers objectAtIndex:1] addSubview:tomorrowWeather]; [self gotWeather]; } else{ NSMutableArray* labels = [[self getLabels] valueForKey:@"values"]; CGRect headerFrame, labelFrame, seperatorFrame; if ([defaults integerForKey:DISPLAY_TYPE]==DisplayTypeNotes) { headerFrame = CGRectMake(0, 0, 320, 60); labelFrame = CGRectMake(10, 30, 300, 30); seperatorFrame = CGRectMake(0, 59, 320, 1); }else{ headerFrame = CGRectMake(0, 0, 320, 68); labelFrame = CGRectMake(10, 38, 300, 30); seperatorFrame = CGRectMake(0, 67, 320, 1); } for(int i=0;i<[labels count];i++){ UIView* header = [[UIView alloc] initWithFrame:headerFrame]; header.backgroundColor = [UIColor whiteColor]; UILabel* lbl = [[UILabel alloc] initWithFrame:labelFrame]; lbl.tag = 2; lbl.font = XX_LARGE_VOLKS_FONT; [lbl setBackgroundColor:[UIColor clearColor]]; [lbl setTextColor:[UIColor darkGrayColor]]; [lbl setText:[labels objectAtIndex:i]]; [header addSubview:lbl]; UIImageView *img; img = [[UIImageView alloc] initWithFrame:seperatorFrame]; [img setBackgroundColor:[UIColor darkGrayColor]]; img.alpha = 0.5; img.tag = 3; [header addSubview:img]; [header bringSubviewToFront:lbl]; [headers addObject:header]; }