【iOS開發】關於UITableView的cell迴圈使用

來源:互聯網
上載者:User

標籤:

如果沒有讓cell迴圈使用的話。每次展示資料就會有一個cell重新建立,並且如果展示過的資料再次展示還是會重新建立一cell,這樣就會造成記憶體的浪費。


解決方案:就是讓展示過的cell放入cel池中,每次需要用的時候就拿出來,換一下內容展示出來,滑動到螢幕以外就把這個cell再次放入cell池中等待下一個展示資料用。所以螢幕顯示幾個cell,這是cell一共需要建立這個多個+1。

比如中:螢幕能完整顯示11個cell,那麼就需要建立12的cell。

如果上上滑動,暴走蘿莉出了螢幕,那麼顯示暴走蘿莉這個cell就沒用,被存入cell池中,那麼顯示影流之主下面那個英雄的cell不會被建立,而是拿出上個cell池中得cell更改一下資料顯示出來就行了,如此迴圈下去。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *ID = @"hero";    //從池中取出資料    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];    if(cell == nil) {        //建立cell,沒用的時候就放入ID池中        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];    }    Hero *hero = self.heros[indexPath.row];        cell.textLabel.text = hero.name;    cell.detailTextLabel.text = hero.intro;    cell.imageView.image = [UIImage imageNamed:hero.icon];        return cell;}


【iOS開發】關於UITableView的cell迴圈使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.