自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone動態(tài)加載圖片 實(shí)例講解

移動開發(fā) iOS
本文介紹的是iPhone動態(tài)加載圖片 實(shí)例講解,如何實(shí)現(xiàn),我們先來看內(nèi)容。

iPhone動態(tài)加載圖片 實(shí)例講解是本文介紹的內(nèi)容。不多說了,先來看內(nèi)容。官方的例子(支持3.x以上的機(jī)子)

http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html

其實(shí)在iphone上面是實(shí)現(xiàn)圖片的動態(tài)加載,其實(shí)也不是很難,其中只要在代理中實(shí)現(xiàn)方法就可以,首先在頭文件中聲明使用到的代理:如 

  1. @interface XXX : UIViewController<UIScrollViewDelegate> 

然后在.m中實(shí)現(xiàn)

  1. //滾動停止的時候在去獲取image的信息來顯示在UITableViewCell上面  
  2. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate  
  3. {  
  4.     if (!decelerate)  
  5. {  
  6.         [self loadImagesForOnscreenRows];  
  7.     }  
  8. }  
  9. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView  
  10. {  
  11.     [self loadImagesForOnscreenRows];  
  12. }  
  13. //  
  14. - (void)loadImagesForOnscreenRows  
  15. {  
  16.     if ([self.entries count] > 0)  
  17.     {  
  18.         NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];  
  19.         for (NSIndexPath *indexPath in visiblePaths)  
  20.         {  
  21.             AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];  
  22.             if (!appRecord.appIcon) // avoid the app icon download if the app already has an icon  
  23.             {  
  24.                 [self startIconDownload:appRecord forIndexPath:indexPath];  
  25.             }  
  26.         }  
  27.     }  
  28. }  
  29. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  30. {  
  31. ………//初始化UITableView的相關(guān)信息  
  32.      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  33.  
  34.     if (cell == nil)  
  35. {  
  36.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  
  37.  
  38.   reuseIdentifier:CellIdentifier] autorelease];  
  39. cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  40.     }  
  41. ……  
  42.      if (!appRecord.appIcon)//當(dāng)UItableViewCell還沒有圖像信息的時候  
  43.         {  
  44.             if (self.tableView.dragging == NO && self.tableView.decelerating == NO)//table停止不再滑動的時候下載圖片(先用默認(rèn)的圖片來代替Cell的image)  
  45.             {  
  46.                 [self startIconDownload:appRecord forIndexPath:indexPath];  
  47.             }  
  48.             cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];                  
  49.         }  
  50.         else//當(dāng)appReacord已經(jīng)有圖片信息的時候直接顯示  
  51.         {  
  52.    cell.imageView.image = appRecord.appIcon;  
  53.         }   

以上就是動態(tài)加載的主要的代碼實(shí)現(xiàn)(其中不包括從網(wǎng)絡(luò)上面下載圖片信息等操作)

因?yàn)槲覀儎?chuàng)建UITableviewCell的時候是以重用的方式來創(chuàng)建,所以就相當(dāng)于說***屏顯示的cell就是以后顯示數(shù)據(jù)和圖片的基礎(chǔ),因?yàn)楹竺鏀?shù)據(jù)超出一平的時候,我們只是改變數(shù)據(jù)的顯示,并沒有為每一個cell的數(shù)據(jù)元創(chuàng)建相應(yīng)的一個

UITableViewCell(這樣非常的浪費(fèi)內(nèi)存),要是我們沒有實(shí)現(xiàn)

  1. (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView  
  2. 和  
  3. (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 

代理的時候,當(dāng)我們滾動UITableView的時候TableView 就是按照順序來加載圖片的信息資源,這樣當(dāng)我們用力滾動Table的時候就感覺相當(dāng)?shù)目?,(其?shí)UITableView實(shí)在一個個的顯示出cell的信息)

當(dāng)我們實(shí)現(xiàn)了以上代理的話,就可以實(shí)現(xiàn)在tableView滾動停止的時候,在去加載數(shù)據(jù)信息,這樣滾動期間的tableViewCell就可以用默認(rèn)的圖片信息來顯示了。

iPhone動態(tài)加載圖片 實(shí)例講解

小結(jié):iPhone動態(tài)加載圖片 實(shí)例講解的內(nèi)容介紹完了,希望本文對你有所幫助!

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-07-25 15:32:06

iPhone Table 動態(tài)

2011-07-21 17:35:11

iPhone Table 圖片

2011-08-22 13:46:15

iPhone開發(fā)GameKit 藍(lán)牙

2009-09-15 09:45:23

Linq動態(tài)條件

2011-07-27 17:07:06

iPhone 游戲 Cocos2d

2015-08-25 10:28:38

前端圖片延遲加載

2011-07-21 17:29:42

iPhone Sqlite 數(shù)據(jù)庫

2011-07-26 13:23:14

iPhone 圖片 相冊

2010-06-03 18:22:38

Hadoop

2011-04-02 16:37:26

PAT

2010-09-14 17:20:57

2011-07-25 18:02:51

iPhone LibFetion 移植

2011-07-27 17:45:29

iPhone 模擬器 圖片

2011-08-08 16:56:44

iPhone 字符處理 視圖

2010-09-03 10:23:49

PPP Multili

2011-04-01 09:04:09

RIP

2011-04-02 16:33:33

2011-08-22 11:49:20

iPhone文件系統(tǒng)NSFileManag

2011-07-29 13:27:48

iPhone 開發(fā) Nib

2011-07-26 16:43:59

iPhone Web 服務(wù)器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號