2011-06-02 50 views
0


由於我是新的iPad應用程序開發請幫助我簡單的問題。
我想在服務器的tableview中顯示圖像。它顯示正確。
但是,當我滾動表上升,再次回到那裏,它會再次從服務器下載圖像數據。
請幫幫我。NSOperation執行uitableview細胞圖像

編輯:
的tableView:的cellForRowAtIndexPath:

static NSString *CellIdentifier = @"cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; 
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 75)]; 
[imgView setImage:[UIImage imageNamed:@"strip_normal.png"]]; 
[imgView setHighlightedImage:[UIImage imageNamed:@"strip_hover.png"]]; 
[cell addSubview:imgView]; 
[imgView release]; 

UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 64, 64)]; 
imgView1.tag = indexPath.row; 
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abc.com/abc/%@.png", [[arrMyCourses objectAtIndex:indexPath.row] valueForKey:@"CourseNumber"]]]]]; 
[imgView1 setImage:img]; 
[cell addSubview:imgView1]; 
[imgView1 release]; 

return cell; 

在此先感謝。

+0

你現在怎麼實現你的'tableView:cellForRowAtIndexPath:'? – 2011-06-02 14:22:17

+0

@Deepak,請參閱我編輯的問題。 – 2011-06-03 04:58:34

回答

1

你的問題是此行

UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abc.com/abc/%@.png", [[arrMyCourses objectAtIndex:indexPath.row] valueForKey:@"CourseNumber"]]]]]; 

每次您正在下載圖像的電池用表視圖請求,相信我,這種情況往往是表視圖不會構建整個視圖在一個單一的射擊。它重新使用屏幕外的單元格來呈現新的可見單元格。所以一旦細胞熄滅並重新開始,cellForRowAtIndexPath:被再次調用。所以圖像會再次下載。您也正在同步下載圖像,這也將阻止用戶界面。

要解決這個問題,您應該考慮在開始時將它們下載一次,並將它們保存在臨時位置並根據需要將它們加載到內存中。把它們全都放在內存中可能會很昂貴。此外,使用performSelectorInBackground:withObject將您的下載移動到背景。您必須將UIKit更新發送回主線程,否則您將遇到崩潰。

+0

你是否有任何示例代碼可用於此,如果是的話,然後提供給我或我已經做到了,但我不知道如何發送單元格位置以及特定單元格的URL。感謝您的回覆。 – 2011-06-03 05:37:37

+1

網上有很多可用資源顯示如何正確執行此操作。來自Apple示例代碼的['Here''](http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html)。你也可以看看['this'](https://github.com/akosma/async-uitableview/)和['this'](http://iphonedevelopment.blogspot.com/2010/05/downloading-images - 用於表-without.html)。 – 2011-06-03 05:45:10

+0

謝謝,我得到了我的答案。 – 2011-06-04 09:01:46