ios ableviewcell的動態載入資料,模仿喜馬拉雅動態資料載入

來源:互聯網
上載者:User

標籤:

 


iphone(UITableViewCell)動態載入圖片
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就可以用預設的圖片資訊來顯示了。

ios ableviewcell的動態載入資料,模仿喜馬拉雅動態資料載入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.