【问题标题】:how to load the images in my view dynamically in a scroll view in iPhone如何在 iPhone 的滚动视图中动态加载视图中的图像
【发布时间】:2010-10-30 15:40:34
【问题描述】:

我想在滚动视图中显示许多图像,例如缩略图,并且我希望动态显示图像,我们像表格视图单元格一样向下或向左滚动

你能告诉我怎么做吗...

谢谢

使用以下代码..当我们滚动滚动视图时,我会调用此代码并能够动态显示图像(仅可见),但问题是..当使用滚动条滚动时,我会得到两个图像..垂直和水平..只有当我滚动时才会发生..任何人都可以帮助我吗..?

int tileSize;
    int imgSize;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        tileSize = 255;
        imgSize = 247;
    }else{
        tileSize = 120;
        imgSize = 116;
    }
    CGRect visibleBounds = [songsContainer bounds];
    for (UIView *tile in [songsContainer subviews]) {
        CGRect scaledTileFrame =[tile frame];
        if (! CGRectIntersectsRect(scaledTileFrame, visibleBounds)) {
            for(UIView *view in [tile subviews])
                [view removeFromSuperview];
            [recycledCells addObject:tile];
            [tile removeFromSuperview];
        }
    }
    int maxRow =[songsDict count]-1; // this is the maximum possible row
    int maxCol = noOfRowsInCell-1;  // and the maximum possible column
    int firstNeededRow = MAX(0, floorf(visibleBounds.origin.y / tileSize));
    int firstNeededCol = MAX(0, floorf(visibleBounds.origin.x / tileSize));
    int lastNeededRow  = MIN(maxRow, floorf(CGRectGetMaxY(visibleBounds) / tileSize));
    int lastNeededCol  = MIN(maxCol, floorf(CGRectGetMaxX(visibleBounds) / tileSize));
    NSLog(@".........MaxRow-%d,MaxCol-%d,firstNeddedRow-%d,firstNeededcol-%d,lNR-%d,lNC%d",maxRow, maxCol, firstNeededRow,firstNeededCol,lastNeededRow,lastNeededCol);
    // iterate through needed rows and columns, adding any tiles that are missing
    for (int row = firstNeededRow; row <= lastNeededRow; row++) {
        NSMutableArray *tempArray = (NSMutableArray *)[songsDict objectAtIndex:row];
        for (int col = firstNeededCol; col <= lastNeededCol ; col++) {
            BOOL tileIsMissing = (firstVisibleRow > row || firstVisibleColumn > col || 
                                  lastVisibleRow  < row || lastVisibleColumn  < col);
            if (tileIsMissing) {
                UIView *tile = (UIView *)[self dequeueReusableTile];

                if (!tile) {
                    // the scroll view will handle setting the tile's frame, so we don't have to worry about it
                    tile = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; 
                    tile.backgroundColor = [UIColor clearColor];
                }
                //tile.image = image for row and col;
                // set the tile's frame so we insert it at the correct position
                CGRect frame = CGRectMake(tileSize * col, tileSize * row, imgSize, imgSize);
                tile.frame = frame;
                if(col<[tempArray count])
                    [self addContentForTile:tile:row:col];
                else tile.backgroundColor = [UIColor clearColor];
                [songsContainer addSubview:tile];

            }
        }
    }
    firstVisibleRow = firstNeededRow+1; firstVisibleColumn = firstNeededCol+1;
    lastVisibleRow  = lastNeededRow;  lastVisibleColumn  = lastNeededCol;

【问题讨论】:

    标签: iphone image uiscrollview scroll dynamic


    【解决方案1】:

    您要做的就是随心所欲地创建滚动视图。您必须决定是否需要网格布局或线性布局。此外,如果是网格,您是否希望它锁定到水平边界,锁定到垂直,所以它可以分别垂直或水平滚动。

    一旦你把它整理好,那么我建议采用类似于 tableview 功能的架构。也就是说,创建单独的“单元格”来保存您的缩略图。一旦有了这些单元格,就可以将它们添加为滚动视图的子视图,在某些偏移处(您需要在 x/y 平面上进行一些数学运算)。

    【讨论】:

    • 但是亚马逊的 WindowShop ipad 应用程序可以垂直和水平滚动...它们是动态加载的吧?
    • 您不必锁定任何一个轴,通常情况下,在缩略图视图中,一个锁定滚动视图中的水平轴或垂直轴。如果您不想锁定任一轴,我所说的仍然适用。
    • 你放置在滚动视图上的东西将是单独的单元格。这些单元格可以是 UIImageView 子类或任何你喜欢的。给它们一个标识符以供重用,然后在您的滚动视图子类中,您需要进行数学运算以按照您的意愿排列单元格。您还需要保留两个NSSets,一个用于回收单元,一个用于可见单元。当您出列一个单元格时,检查回收的单元格,如果 anyObject 不为零,则将其传递给用户,这样您就不必分配一个新的单元格。否则,返回零。随意排列滚动视图中的单元格。
    • 诀窍在于计算可见单元格。您将需要预加载一些不在屏幕上的单元格,以便您的用户在快速向下滚动时看到带有内容的单元格。当一个单元格可见时,将其添加到可见单元格NSSet,当它不可见时,将其从可见单元格集中移除,并将其添加到回收单元格集中。这是细胞重用的症结所在。我会把剩下的留给读者练习。
    • 您好,我按照您所说的进行了尝试,但遇到了一个问题..编辑了这个问题。请告诉我问题..是代码..?
    【解决方案2】:

    从滚动视图开始,将 UIImageView 中的每个图像作为子视图添加到特定位置的滚动视图。

    要记住的一件事是仅将当前显示的图像及其直接邻居保存在内存中。

    【讨论】:

    • 取决于他有多少图像视图,这可能会消耗大量内存。他会想(为了稳健性)研究单元和排队机制
    猜你喜欢
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多