在scrollView上的UiView进行内存警告

时间:2012-03-05 16:15:22

标签: iphone objective-c

我正在使用此代码创建几个视图并将它们添加到滚动视图,在每个视图上我有4个图像,我在循环中设置。 当我完成后,我将视图添加到滚动视图和数组,其中包含我添加到滚动视图的所有视图:

for (int i = 0; i < numOfRows ; i++) {
    AlbumRow *tmp;
    NSArray *topLevelObjects = topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AlbumRow" 
                                                        owner:nil 
                                                      options:nil];

    for (id currentObject in topLevelObjects) {
        if ([currentObject isKindOfClass:[UIView class]]) {
            tmp = (AlbumRow*) currentObject;
            break;
        }
    }

    int y = 0;
    if (i != 0) {
        y = 85 * i;
    }
    [tmp setFrame:CGRectMake(1, y, 320, 85)];

    [tmp setBackgroundColor:[UIColor clearColor]];

    int imgInRow = kItemInRow;

    for (int j = 0; j < imgInRow; j++) {
        if ([album.albumItems count] > current) {
            UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewClicked:)];
            ItemEntity *entity = [album.albumItems objectAtIndex:current];
            switch (j) {
                case 0:
                    tmp.image1.image = [UIImage imageWithContentsOfFile:entity.path];
                    [tmp.image1 setTag:current];
                    [tmp.image1 addGestureRecognizer:gestureRecognizer];
                    break;
                case 1:
                    tmp.image2.image = [UIImage imageWithContentsOfFile:entity.path];
                    [tmp.image2 setTag:current];
                    [tmp.image2 addGestureRecognizer:gestureRecognizer];
                    break;
                case 2:
                    tmp.image3.image = [UIImage imageWithContentsOfFile:entity.path];
                    [tmp.image3 setTag:current];
                    [tmp.image3 addGestureRecognizer:gestureRecognizer];
                    break;
                case 3:
                    tmp.image4.image = [UIImage imageWithContentsOfFile:entity.path];
                    [tmp.image4 setTag:current];
                    [tmp.image4 addGestureRecognizer:gestureRecognizer];
                    break;
                case 4:
                    tmp.image5.image = [UIImage imageWithContentsOfFile:entity.path];
                    [tmp.image5 setTag:current];
                    [tmp.image5 addGestureRecognizer:gestureRecognizer];
                    break;
                default:
                    break;
            }

            current++;
            [gestureRecognizer release];
            tmp.count++;
        }
    }

    [scroll addSubview:tmp];
    [rows addObject:tmp];
}

我需要将它添加到数组中,因为我稍后会在我的课程中使用它们。 这就是我完成后清理内存的方式:

for (AlbumRow *row in rows) {
            [row removeFromSuperview];
        }

        [rows removeAllObjects];

当我在滚动视图中添加了很多项目时,我在这段代码上收到了很多内存警告,我做错了什么?

0 个答案:

没有答案