UIScrollView没有显示第一个对象

时间:2013-04-08 00:21:36

标签: uiscrollview nsmutablearray

我目前有一个从数组加载图像的滚动视图,这是我的代码:

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个,我做错了什么?

1 个答案:

答案 0 :(得分:1)

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

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