2010-09-09 78 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