2010-09-09 77 views
0

我已经把图像放入我的tableview单元格中,图像位于某个URL .. 我的问题是当我的tableView已经显示数据时, 然后滚动该表查看&下来,它会需要一段时间或延迟才会对滚动作出反应。图像在TableView单元格导致延迟滚动

我发现问题出在位于URL中的图像,我还需要显示每个图像tableview单元格。 任何想法如何解决它?还是要进行优化?

这里是我的代码:

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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Set up the cell 
NSUInteger row = [indexPath row]; 
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

NSArray *arrLocal = [m_list objectAtIndex:row]; 
if ([arrLocal count] > 0) { 
    cell.textLabel.text = [arrLocal objectAtIndex:2]; 
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14]; 
    cell.detailTextLabel.text = [arrLocal objectAtIndex:3]; 
    cell.detailTextLabel.font = [UIFont systemFontOfSize:12]; 

      //sample url of the image: http://www.mysite.com/images/DealExtreme/2657_1_small.jpg 
    NSString *strTemp = [[NSString alloc] initWithString:@"http://www.mysite.com/"]; 
    NSString *urlString = [strTemp stringByAppendingString: [arrLocal objectAtIndex:1]]; 
    NSURL *url = [NSURL URLWithString:urlString]; 
    UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; 
    cell.imageView.image = image;  
    [strTemp release]; 
} 

return cell; 
} 

回答

0

的问题是,你正在下载的图像每次cellForRow叫,我建议您加载图像的背景...做的一个方法是使你的自定义单元格,让他们有一种方法或类似的东西,将在后台加载图像,然后设置它,另一种方式可能是将所有图像加载到背景中并保留它们,然后设置单元格图像,这样你的arent重装图像每次细胞接触屏幕的...希望它有助于

+0

是的,这是...谢谢 – 2010-09-10 16:39:06

0

必须异步加载图像数据O你阻止了主线程。你一定要避免这一点。它使你的应用程序无响应,如果延迟足够大,你的应用程序可能很容易被操作系统杀死。

所以开始在后台下载图像(使用NSURLRequestNSURLConnection),并在数据完成时刷新单元格的内容。

+0

感谢您的信息 – 2010-09-10 16:38:48