關(guān)于UITableView的一些性能優(yōu)化
在實際iOS開發(fā)中,我們用的最多和最常見的一個UI空間就是列表(UITableView),在UI空間中設(shè)計到的知識點也是列表最多,比如列表的一系列協(xié)議方法,包括必須實現(xiàn)的和可選實現(xiàn)的。那么在實際開發(fā)中,為了給用戶一個更好的體驗,那么久需要進行性能的一下改善。
UITableView的介紹
UITableView是繼承于UIScrollview的,因此可以自動響應(yīng)滾動事件,UITableView最常用的兩個協(xié)議是:UITableViewDataSource和UITableViewDelegate,對于這兩個協(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;
在實際開發(fā)中,我們需要注意的是創(chuàng)建TableView時,盡可能的用懶加載的創(chuàng)建方式,這樣的話對內(nèi)存占用會有明顯的減少,示例代碼:
- (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;
}
當然在注冊cell的時候,上面是直接給了一個Identifier,其實為了更好的優(yōu)化,建議大家這么寫:
static NSString *Identifier = @"QLTableViewCell";然后在注冊的時候可以直接調(diào)用。
數(shù)據(jù)源數(shù)組,盡可能的也用懶加載,在數(shù)據(jù)請求回來后,進行self.dataArray進行保存。
如果你在開發(fā)的時候?qū)τ贑ell,用的是xib,那么你在這里也可以優(yōu)化,
1、盡可能的避免系統(tǒng)反復(fù)調(diào)用
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
2、在創(chuàng)建cell的時候,避免重復(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進行設(shè)置行高,利用系統(tǒng)提供的屬性,進行自動緩存和計算行高,并進行展示:
/*估算tableView的高度為200*/
self.tableView.estimatedRowHeight = 200;
/*自動計算行高*/
self.tableView.rowHeight = UITableViewAutomaticDimension;
4、模型和視圖進行分開,在進行VC的關(guān)聯(lián),實現(xiàn)MVC開發(fā)模式。
在請求數(shù)據(jù)的時候,異步請求,刷新視圖的數(shù)據(jù)的時候回到主線程刷新,實現(xiàn)高內(nèi)聚,低耦合。