Iphone 如何在后台下载表格视图单元格的多个图像?

Iphone 如何在后台下载表格视图单元格的多个图像?,iphone,uitableview,Iphone,Uitableview,我想在tableView的一行中创建一个应用程序,将有4个按钮的图像,这些图像将在后台从服务器加载,因为如果我直接加载它们,那么tableView将挂起一些。为此,我使用这段代码在单元格中创建按钮,并在后台下载图像 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *hlCellI

我想在tableView的一行中创建一个应用程序,将有4个按钮的图像,这些图像将在后台从服务器加载,因为如果我直接加载它们,那么tableView将挂起一些。为此,我使用这段代码在单元格中创建按钮,并在后台下载图像

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

        static NSString *hlCellID = @"hlCellID";

        UITableViewCell *hlcell = [tableView dequeueReusableCellWithIdentifier:hlCellID];
        if(hlcell == nil) {
            hlcell =  [[[UITableViewCell alloc] 
                        initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hlCellID] autorelease];
            hlcell.accessoryType = UITableViewCellAccessoryNone;
            hlcell.selectionStyle = UITableViewCellSelectionStyleNone;
        }

        int section = indexPath.section;
        NSMutableArray *sectionItems = [sections objectAtIndex:section];

        int n = [sectionItems count];

        int i = 0, j = 0, tag = 1;
        int x = 10;
        int y = 30;

        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        for (i = 1; i<= n ; i++) //[arr count]
        {
            for(j=1; j<=4;j++)
            {

                if (i>=n) break;
                Item *item = [sectionItems objectAtIndex:i];

                CGRect rect = CGRectMake(x = x, y = y, 68, 65); 
                UIButton *button=[[UIButton alloc] initWithFrame:rect];
                [button setFrame:rect];
                UIImage *buttonImageNormal=[UIImage imageNamed:item.image];
                [button setBackgroundImage:buttonImageNormal    forState:UIControlStateNormal];
                [button setContentMode:UIViewContentModeCenter];
                // set the image to be loaded (using the same one here but could/would be different)
                NSURL *imgURL = [NSURL URLWithString:@"http://londonwebdev.com/wp-content/uploads/2010/07/featured_home.png"];      

                // Create an array with the URL and imageView tag to 
                // reference the correct imageView in background thread.
                NSMutableArray *arr = [[NSArray alloc] initWithObjects:imgURL, [NSString stringWithFormat:@"%d", tag], nil  ];

                // Start a background thread by calling method to load the image
                [self performSelectorInBackground:@selector(loadImageInBackground:) withObject:arr];
    //          button.tag = [tagValue intValue];
                button.tag = tag;
                //NSLog(@"....tag....%d", button.tag);

                [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
                [hlcell.contentView addSubview:button];
                [button release];

                tag++;

                x = x + 77; 

            }
            x = 10;
            y = y + 74;
        }
        [pool release];
        return hlcell;
    }
所有的代码都很完美,但是我在这里找不到表单元格子视图
for(UIButton*checkView in[self.tblImage subview]
那么如何找到表单元格子视图呢

之后,新城市将出现,只需更改部分,数据将在行和单元格部分显示新图像。

您可以使用

网络图像 此库为UIImageVIew提供了一个类别,支持来自web的远程图像

它规定:

向Cocoa Touch框架添加web映像和缓存管理的UIImageView类别 异步图像下载程序 具有自动缓存过期处理的异步内存+磁盘映像缓存 保证同一URL不会被多次下载 保证伪造的URL不会被一次又一次地重试 表演

就用它吧

[youImageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil options:SDWebImageRetryFailed];

请查看更新问题并指导我该怎么做?这所有代码在滚动视图中运行良好,但我想在表视图中创建。因为我也想实现搜索功能。苹果示例链接:先生,我看过这个示例,但我认为这不能满足我的问题。请直呼我的名字。如果您有n个图片进入UITable查看然后你必须采用延迟加载的概念。这意味着首先加载所有轻量级组件,如UILabels、UIButton。延迟一段时间后,开始在后台线程中加载图像(最好在0.3秒后)。这样可以获得良好的稳定性能。您能告诉我如何找到表格单元格的子视图吗?如图所示,[self.tblImage subview]中的
UIButton*checkView
我使用过这段代码,但它没有给出表视图行单元格项中的子视图计数。@NaveenThunga感谢我通过传递行索引解决了我的问题,现在我正在为多行Indexath实现。
[youImageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil options:SDWebImageRetryFailed];