iphone - 将图像加载到网格中时遇到问题

标签 iphone objective-c image uitableview

我在从 xml 文件将图像加载到 uitable View 中时遇到问题,我已经正确加载了所有内容,问题出在 uitableview 中,而不是加载图像 0-9,它只加载 0-7,然后加载图像 0 和 1? 附件是项目,任何帮助将不胜感激 谢谢

http://jptarry.com/iPhoneTest/iphoneGrid.zip

最佳答案

我希望我是对的。我现在没有 Mac 来测试这个,但我在代码中看到了一些错误。

在 MainViewController 中,当您重用单元格时,只需返回旧单元格而不进行任何更改。 我想这就是为什么你能取得0-7然后又是0-1的原因。屏幕上很可能有 7 个可见的单元格,当您开始滚动时,您只是返回出队的单元格(它们是 0 和 1,因为您向下滚动并且这些单元格不在屏幕之外)。 重用单元格时,您应该使用当前索引的图像重新初始化 asyncImageView。我将复制粘贴一些代码,但这更多的是作为指导,因为我无法编译/测试它:

此外,您还将相同的标签附加到在同一单元格中创建的多个 asyncImageView。你将无法将它们全部找回来。根据索引或其他内容附加 ids。

编辑: 这就是我让它工作的方式。但是,我真的不明白您的代码背后的逻辑,您在单元格中排列按钮。

此外,AsyncImageView 仍然存在问题。当它加载新图像时,它会显示旧图像 - 这就是为什么当您滚动时,您会看到旧图像首先出现事件指示器,然后出现新图像。您应该有 AsyncImageView 的加载状态,其中显示中性的内容。

另外,我对标记方法非常满意,但它确实有效:) 好的,说得够多了,这是我更改的代码:


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    AsyncImageView *asyncImageView = nil;

    int section = indexPath.section;
    NSMutableArray *sectionItems = [sections objectAtIndex:section];
    int n = ([sectionItems count]) ;


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];           
    if (cell == nil) 
    {
        // Create a new cell
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;


        int i=0; 
        int f=0;
        while (i < [sectionItems count])
        {
            int yy = 4 +f*74;
            //number of items per section 2
            for(int j=0; j<[sectionItems count]; j++)
            {

                Item *item = [sectionItems objectAtIndex:j];

                int buttonX = 10;
                if (j != 0)
                    buttonX = 203;


                NSLog(@"+++++++++++++++++++++++++++++++");
                NSLog(@"image :: %d :: %@", j, item.image);
                NSLog(@"+++++++++++++++++++++++++++++++");

                CGRect frame;
                frame.origin.x = buttonX;
                frame.origin.y = yy;
                frame.size.width = 107;
                frame.size.height = 107;
                asyncImageView = [[[AsyncImageView alloc] initWithFrame:frame] autorelease];
                asyncImageView.backgroundColor = [UIColor clearColor];
                asyncImageView.tag = ASYNC_IMAGE_TAG + f * 100 + j;
                //asyncImageView.opaque = YES; 
                [cell.contentView addSubview:asyncImageView];


                i++;
            }
            f = f+1;
        }
    }
    else 
    {
        NSLog(@"cell != nill %@", cell);
    }


    int i=0; 
    int f=0;
    while (i < [sectionItems count])
    {
        //number of items per section 2
        for(int j=0; j<[sectionItems count]; j++)
        {

            Item *item = [sectionItems objectAtIndex:j];

            asyncImageView = (AsyncImageView *) [cell.contentView viewWithTag:ASYNC_IMAGE_TAG + f * 100 + j];   
            [asyncImageView loadImageFromURL:[NSURL URLWithString:item.image]];

            i++;
        }
        f = f+1;
    }



    return cell;
}


EDIT2:我对 AsyncImageView 进行了快速修复,以便在加载新图像时删除旧图像。我将发布一些带有更改的代码摘录:



-(void)loadImageFromURL:(NSURL*)url {

    // ADD THE FOLLOWING LINES
    // Remove the old image (tagged with 1234)  
    [[self viewWithTag:1234] removeFromSuperview];
    // END ADD


    if (connection != nil) {
        NSLog(@"connection != nil");
        [connection cancel];
        [connection release];

在同一方法中稍低一点,从缓存创建图像,添加这些行:


        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.autoresizingMask = 
        UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        // ADD THE NEXT LINE
    imageView.tag = 1234;    // add this line
        // END ADD
        [self addSubview:imageView];

在另一个函数 - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection 中创建新图像,只需再次添加标签即可:



    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.autoresizingMask = 
    UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    // ADD THE NEXT LINE
    imageView.tag = 1234;
    // END ADD
    [self addSubview:imageView];
    imageView.frame = self.bounds;

关于iphone - 将图像加载到网格中时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5074554/

相关文章:

iphone - 单击 iPhone 中的按钮更改本地化的 nib 文件

iphone - 境界+快速排序

javascript - 如何从 Javascript 调用原生 Iphone/Android 函数?

ios - 如何在 TableViewController 中使用 indexPathForItemAtPoint?

objective-c - 无缝循环视频

iOS Cocos2D优化

wpf - 禁用元素时按钮上的图像变灰(简单美观的方式)

ios - 如何让 Xcode 6 重新生成配置文件

javascript - JS Dice - 使用 if 条件将 Math.random 值分配给 html 图像

iphone - 自定义 UITableViewCell 背景,何时重新加载等