UITableView的优化原理
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
當(dāng)我們下啦一個(gè) UITableView時(shí),如果沒(méi)有做優(yōu)化,只是簡(jiǎn)單的實(shí)現(xiàn)功能代碼如下,這樣當(dāng)我們有上百條tableviewcell的時(shí)候,我們滑動(dòng)的非常快時(shí)會(huì)非常費(fèi)內(nèi)存,當(dāng)然蘋(píng)果公司不會(huì)讓我們這樣干,蘋(píng)果公司會(huì)在程序啟動(dòng)加載頁(yè)面的時(shí)候,只開(kāi)辟出現(xiàn)在頁(yè)面上的tableviewcell,剩下的就需要你滑動(dòng)到該條才加載到內(nèi)存中,已經(jīng)劃出的uitableviewcell則放到tableview內(nèi)存池中,當(dāng)下面需要這個(gè)類(lèi)型的tableviewcell時(shí)就加載進(jìn)去,
UITableViewCell對(duì)象的重用原理 iOS設(shè)備的內(nèi)存有限,如果用UITableView顯示成千上萬(wàn)條數(shù)據(jù),就需要成千上萬(wàn)個(gè)UITableViewCell對(duì)象的話,那將會(huì)耗盡iOS設(shè)備的內(nèi)存。要解決該問(wèn)題,需要重用UITableViewCell對(duì)象 重用原理:當(dāng)滾動(dòng)列表時(shí),部分UITableViewCell會(huì)移出窗口,UITableView會(huì)將窗口外的UITableViewCell放入一個(gè)對(duì)象池中,等待重用。當(dāng)UITableView要求dataSource返回UITableViewCell時(shí),dataSource會(huì)先查看這個(gè)對(duì)象池,如果池中有未使用的UITableViewCell,dataSource會(huì)用新的數(shù)據(jù)配置這個(gè)UITableViewCell,然后返回給UITableView,重新顯示到窗口中,從而避免創(chuàng)建新對(duì)象 還有一個(gè)非常重要的問(wèn)題:有時(shí)候需要自定義UITableViewCell(用一個(gè)子類(lèi)繼承UITableViewCell),而且每一行用的不一定是同一種UITableViewCell(如短信聊天布局),所以一個(gè)UITableView可能擁有不同類(lèi)型的UITableViewCell,對(duì)象池中也會(huì)有很多不同類(lèi)型的UITableViewCell,那么UITableView在重用UITableViewCell時(shí)可能會(huì)得到錯(cuò)誤類(lèi)型的UITableViewCell 解決方案:UITableViewCell有個(gè)NSString *reuseIdentifier屬性,可以在初始化UITableViewCell的時(shí)候傳入一個(gè)特定的字符串標(biāo)識(shí)來(lái)設(shè)置reuseIdentifier(一般用UITableViewCell的類(lèi)名)。當(dāng)UITableView要求dataSource返回UITableViewCell時(shí),先通過(guò)一個(gè)字符串標(biāo)識(shí)到對(duì)象池中查找對(duì)應(yīng)類(lèi)型的UITableViewCell對(duì)象,如果有,就重用,如果沒(méi)有,就傳入這個(gè)字符串標(biāo)識(shí)來(lái)初始化一個(gè)UITableViewCell對(duì)象
<!-- lang: cpp --> UITableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"ci"];NSString *str = [NSString stringWithFormat:@"我是cell塊——————%d",indexPath.row]; NSLog(@"%d------%p",indexPath.row,tableViewCell); tableViewCell.textLabel.text = str;
下面是優(yōu)化好的代碼:
<!-- lang: cpp -->
//// pyViewController.m // 1128-05UITableView的優(yōu)化設(shè)計(jì) // // Created by panyong on 13-11-28. // Copyright (c) 2013年 panyong. All rights reserved. //
#import "pyViewController.h"
@interface pyViewController ()<UITableViewDataSource,UITableViewDelegate>
@end @implementation pyViewController
- (void)viewDidLoad { [super viewDidLoad]; //設(shè)置tableView類(lèi)型,UITableViewStyleGrouped和UITableViewStylePlain類(lèi)型
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; [self.view addSubview:tableView]; tableView.dataSource = self;//設(shè)置數(shù)據(jù)源代理 設(shè)置tableviewcell內(nèi)的數(shù)據(jù) tableView.delegate = self;//設(shè)置代理 設(shè)置tableviewcell的高度 }
// 返回cell的行數(shù) -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 70; }
//返回cell -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //注意此處生成的cell的類(lèi)型ci類(lèi)型,ci可以隨便寫(xiě)! UITableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"ci"]; //這里我循環(huán)得到70行,注意內(nèi)存哦!!!! NSString *str = [NSString stringWithFormat:@"我是cell塊——————%d",indexPath.row]; if (tableViewCell == nil) { //tableview內(nèi)存池,當(dāng)有不用的tableviewcell劃出屏幕時(shí),就被回收到內(nèi)存池中,然后,下面的tableviewcell從下面劃出時(shí)是需要開(kāi)辟tableviewcell的,所以以下tableviewcell先要判斷類(lèi)型reuseIdentifier是不是呵上面的tableviewcell相同,就好像一個(gè)病人要補(bǔ)充血液要找到自己合適的血型一樣,如果相符就使用內(nèi)存池里的,不果不相符系統(tǒng)重新開(kāi)辟一個(gè)此種類(lèi)型的tableviewcell 所以下面打印的時(shí)候地址是循環(huán)相同的,0---3都是不一樣的地址,然后才是重復(fù)0---3的地址!!!! tableViewCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ci"]; } //iphone官方加載機(jī)制是出現(xiàn)再屏幕上的內(nèi)容(如tableViewCell)才加載,所以注意此處的打印!!!!一開(kāi)始的時(shí)候由于我設(shè)置了cell的高度,一個(gè)3.5寸的屏幕只有顯示三條cell, NSLog(@"%d------%p",indexPath.row,tableViewCell); tableViewCell.textLabel.text = str;
return tableViewCell;}
//dele的一個(gè)方法返回cell的高度,該方法是實(shí)現(xiàn)協(xié)議UITableViewDelegate的方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 180;//這里為了方便演示效果,所以設(shè)置了cell的高度 }
@end
屏幕顯示結(jié)果如下 打印內(nèi)存結(jié)果如下,! 在此輸入圖片描述
其實(shí)我們應(yīng)該好好理解以下內(nèi)存池的概念,蘋(píng)果公司為了性能考慮,設(shè)計(jì)的這個(gè)理念!!!! tableViewCell.textLabel.text = str;這一句你可以放入內(nèi)存池中試一下結(jié)果,呵呵,想清楚了嗎????
轉(zhuǎn)載于:https://my.oschina.net/panyong/blog/179949
總結(jié)
以上是生活随笔為你收集整理的UITableView的优化原理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ansys选择一个面上所有节点_ansy
- 下一篇: 77GHz毫米波雷达快速chirp信号技