2013-04-08 46 views
1

我现在有一个从阵列滚动视图加载图像,这里是我的代码:的UIScrollView没有显示第一对象

icons = [[NSMutableArray alloc]initWithObjects:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"one" ofType:@"png"]], [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"two" ofType:@"png"]], [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"three" ofType:@"png"]],nil]; 

int numberOfItems = [icons count]; 
NSLog(@"Number of objects: %i",numberOfItems); 

for (int i = 0; i < numberOfItems; i++) { 

    if (i <= 3) { 

     finalFrame = CGRectMake(i*(80), 12, 80, 80); 
    } 

    UIButton *iconButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    iconButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    iconButton.frame = CGRectMake(0, 0, 57, 57); 
    iconButton.center = CGPointMake(40, 29); 
    iconButton.imageView.contentMode = UIViewContentModeScaleToFill; 
    [iconButton addTarget:self action:@selector(selected:) forControlEvents:UIControlEventTouchUpInside]; 
    iconButton.tag = i; 

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    dispatch_async(queue, ^{ 
     UIImage *icon = [icons objectAtIndex:i]; 
      dispatch_sync(dispatch_get_main_queue(), ^{ 
      [iconButton setImage:icon forState:UIControlStateNormal]; 
     }); 
    }); 

    [sub addSubview:iconButton]; 

    sub = [[UIView alloc] initWithFrame:finalFrame]; 
    [scrollView addSubview:sub]; 
} 

的NSLog的读取3个对象,但我的滚动视图只显示他们的2,什么是我做错了?

+0

是您的代码给对象的权数? – 2013-04-08 06:18:33

+0

是的,日志读取数组中的三个对象,其中有。 – 2013-04-08 21:33:47

回答

1

我认为它是在for循环应该是这样的

(int i = 0; i <= numberOfItems; i++) 
+0

不,看到你的回答后,我以为你是对的,但是一旦我改变了它,就会抛出一个超出索引的错误。 – 2013-04-08 21:33:10