2013-04-08 66 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