iOS-UITableView的優(yōu)化

關(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)聚,低耦合。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,289評論 3 38
  • TableView的優(yōu)化 一:什么是TableView的優(yōu)化以及為什么要優(yōu)化 1)CPU(中央處理器)和GPU(圖...
    學(xué)_iOS閱讀 1,166評論 0 6
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,602評論 30 472
  • 用到的組件 1、通過CocoaPods安裝 2、第三方類庫安裝 3、第三方服務(wù) 友盟社會化分享組件 友盟用戶反饋 ...
    SunnyLeong閱讀 15,158評論 1 180
  • 在學(xué)習(xí)稻盛和夫經(jīng)營哲學(xué)中,讓我深深反思的問題: 什么樣的人是真正有實力的人? 擁有恪守職守的能力,同時人格高尚,值...
    靜尚形象設(shè)計閱讀 523評論 0 0

友情鏈接更多精彩內(nèi)容