官方的例子(支援3.x以上的機子)
http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html
其實在iphone上面是實現圖片的動態載入,其實也不是很難,其中只要在代理中實現方法就可以
首先在標頭檔中聲明使用到的代理 如
@interface XXX : UIViewController<UIScrollViewDelegate>
然後在.m中實現
//滾動停止的時候在去擷取image的資訊來顯示在UITableViewCell上面
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (!decelerate)
{
[self loadImagesForOnscreenRows];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self loadImagesForOnscreenRows];
}
//
- (void)loadImagesForOnscreenRows
{
if ([self.entries count] > 0)
{
NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
for (NSIndexPath *indexPath in visiblePaths)
{
AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
if (!appRecord.appIcon) // avoid the app icon download if the app already has an icon
{
[self startIconDownload:appRecord forIndexPath:indexPath];
}
}
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
………//初始化UITableView的相關資訊
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
………
if (!appRecord.appIcon)//當UItableViewCell還沒有映像資訊的時候
{
if (self.tableView.dragging == NO && self.tableView.decelerating == NO)//table停止不再滑動的時候下載圖片(先用預設的圖片來代替Cell的image)
{
[self startIconDownload:appRecord forIndexPath:indexPath];
}
cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];
}
else//當appReacord已經有圖片資訊的時候直接顯示
{
cell.imageView.image = appRecord.appIcon;
}
}
以上就是動態載入的主要的代碼實現(其中不包括從網路上面下載圖片資訊等操作)
*************************************
因為我們建立UITableviewCell的時候是以重用的方式來建立,所以就相當於說第一屏顯示的cell就是以後顯示資料和圖片的基礎,因為後面資料超出一平的時候,我們只是改變資料的顯示,並沒有為每一個cell的資料元建立相應的一個
UITableViewCell(這樣非常的浪費記憶體),要是我們沒有實現
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
和
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
代理的時候,當我們滾動UITableView的時候TableView 就是按照順序來載入圖片的資訊資源,這樣當我們用力滾動Table的時候就感覺相當的卡,(其實UITableView實在一個個的顯示出cell的資訊)
當我們實現了以上代理的話,就可以實現在tableView滾動停止的時候,在去載入資料資訊,這樣滾動期間的tableViewCell就可以用預設的圖片資訊來顯示了。