關(guān)于UITableView的一些性能優(yōu)化
在實(shí)際iOS開發(fā)中庶艾,我們用的最多和最常見的一個(gè)UI空間就是列表(UITableView)袁余,在UI空間中設(shè)計(jì)到的知識(shí)點(diǎn)也是列表最多,比如列表的一系列協(xié)議方法落竹,包括必須實(shí)現(xiàn)的和可選實(shí)現(xiàn)的泌霍。那么在實(shí)際開發(fā)中货抄,為了給用戶一個(gè)更好的體驗(yàn)述召,那么久需要進(jìn)行性能的一下改善。
UITableView的介紹
UITableView是繼承于UIScrollview的蟹地,因此可以自動(dòng)響應(yīng)滾動(dòng)事件积暖,UITableView最常用的兩個(gè)協(xié)議是:UITableViewDataSource和UITableViewDelegate,對(duì)于這兩個(gè)協(xié)議里面的常用方法:
UITableViewDataSource的常用方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@optional
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // fixed font style. use custom view (UILabel) if you want something different
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
在實(shí)際開發(fā)中怪与,我們需要注意的是創(chuàng)建TableView時(shí)夺刑,盡可能的用懶加載的創(chuàng)建方式,這樣的話對(duì)內(nèi)存占用會(huì)有明顯的減少分别,示例代碼:
- (UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[_tableView registerNib:[UINib nibWithNibName:@"QLTableViewCell" bundle:nil] forCellReuseIdentifier:@"tableViewCell"];
[_tableView registerNib:[UINib nibWithNibName:@"QLheaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"tableHeaderView"];
_tableView.backgroundColor = [UIColor clearColor];
}
return _tableView;
}
當(dāng)然在注冊(cè)cell的時(shí)候遍愿,上面是直接給了一個(gè)Identifier,其實(shí)為了更好的優(yōu)化,建議大家這么寫:
static NSString *Identifier = @"QLTableViewCell";然后在注冊(cè)的時(shí)候可以直接調(diào)用耘斩。
數(shù)據(jù)源數(shù)組沼填,盡可能的也用懶加載,在數(shù)據(jù)請(qǐng)求回來后括授,進(jìn)行self.dataArray進(jìn)行保存坞笙。
如果你在開發(fā)的時(shí)候?qū)τ贑ell,用的是xib荚虚,那么你在這里也可以優(yōu)化薛夜,
1、盡可能的避免系統(tǒng)反復(fù)調(diào)用
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
2版述、在創(chuàng)建cell的時(shí)候梯澜,避免重復(fù)創(chuàng)建,可以利用復(fù)用:
pragma mark - 協(xié)議方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
QLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell" forIndexPath:indexPath];
//去掉cell選中的背景顏色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.model = self.dataArray[indexPath.row];
return cell;
}
3渴析、不使用1進(jìn)行設(shè)置行高晚伙,利用系統(tǒng)提供的屬性简十,進(jìn)行自動(dòng)緩存和計(jì)算行高,并進(jìn)行展示:
/*估算tableView的高度為200*/
self.tableView.estimatedRowHeight = 200;
/*自動(dòng)計(jì)算行高*/
self.tableView.rowHeight = UITableViewAutomaticDimension;
4撬腾、模型和視圖進(jìn)行分開螟蝙,在進(jìn)行VC的關(guān)聯(lián),實(shí)現(xiàn)MVC開發(fā)模式民傻。
在請(qǐng)求數(shù)據(jù)的時(shí)候胰默,異步請(qǐng)求,刷新視圖的數(shù)據(jù)的時(shí)候回到主線程刷新漓踢,實(shí)現(xiàn)高內(nèi)聚牵署,低耦合。