1

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

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

4

1 回答 1

1

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

(int i = 0; i <= numberOfItems; i++)
于 2013-04-08T04:54:09.503 回答