2
votes

I'm using iCarousel to display information. Each index (7 total) has a unique image with a label over it. Each is set with an if statement if (index == 0){...} etc.

However when I load the page (and the carousel) only the first three images load initially. The system uses lazy loading to wait until the carousel is turned to grab the next images. I can prove through print statements that the index does infact increase past 2 all the way up to 6 [0..6]. But for some reason my carousel images repeat after the third one.

Example: Monday, Tuesday, Wednesday, Monday, Tuesday, Wednesday, Monday Should be: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

Any ideas how to fix this?

MORE TESTS For Index 0-2 the view is nil, but for index 3+ the view is not nil and the if skipped. Any idea how to fix that?

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view{

UILabel *label = nil;
NSLog(@"INDEX is %i", index);
//create new view if no view is available for recycling
if (view == nil)
{
    view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0, 200.0)];
    //view = [[UIImageView alloc] init];
    ((UIImageView *)view).image = [UIImage imageNamed:_locationDetailModel[2]];
    view.contentMode = UIViewContentModeCenter;
    label = [[UILabel alloc] initWithFrame:view.bounds];
    label.minimumScaleFactor = 8./label.font.pointSize;
    label.adjustsFontSizeToFitWidth = YES;
    [label setFont:[UIFont boldSystemFontOfSize:12]];
    ...
    if (index == 0){
        label.backgroundColor = [UIColor colorWithPatternImage:[self resizeForCarousel:[UIImage imageNamed:@"Monday_Hours.jpg"]]];
        }
    } else if (index == 1) {
        label.backgroundColor = [UIColor colorWithPatternImage:[self resizeForCarousel:[UIImage imageNamed:@"Tuesday_Hours.jpg"]]];
    } else if (index == 2){
        label.backgroundColor = [UIColor colorWithPatternImage:[self resizeForCarousel:[UIImage imageNamed:@"Wednesday_Hours.jpg"]]];
    } else if (index == 3){
        label.backgroundColor = [UIColor colorWithPatternImage:[self resizeForCarousel:[UIImage imageNamed:@"Thursday_Hours.jpg"]]];
    } else if (index == 4){
        label.backgroundColor = [UIColor colorWithPatternImage:[self resizeForCarousel:[UIImage imageNamed:@"Friday_Hours.jpg"]]];
    } else if (index == 5){
        label.backgroundColor = [UIColor colorWithPatternImage:[self resizeForCarousel:[UIImage imageNamed:@"Saturday_Hours.jpg"]]];
    } else if (index == 6){
        label.backgroundColor = [UIColor colorWithPatternImage:[self resizeForCarousel:[UIImage imageNamed:@"Sunday_Hours.jpg"]]];
    }
    label.textAlignment = NSTextAlignmentCenter;
    //label.textAlignment = UITextAlignmentCenter;
    label.font = [label.font fontWithSize:50];
    label.tag = 1;
    [view addSubview:label];
}
else
{
    label = (UILabel *)[view viewWithTag:1];
}
return view;

}

1
SOLVED: Removed the if (view == nil). Is this a bad approach?Marcus
Add this as an answer, accept and close the question.rishi

1 Answers

1
votes

The key is to remove if(view == nil)

The first 3 index points are valid, but after that they are initiated and no longer nil