【IOS開發】UITableView的效能最佳化

來源:互聯網
上載者:User

標籤:

一、.重用cell

  在資料來源方法中,在可見的頁面重複繪製

OC方法中- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;SWIFT方法override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

 因此 ,可以在該方法中做一些處理操作 

OC處理操作 static NSString *  ReusedCellID = "ReusedCellID";  UITableViewCell * cell = tableView.dequeueReusableCellWithIdentifier( ReusedCellID, forIndexPath: indexPath)swift的處理操作 private let ReusedCellID = "ReusedCellID" let cell = tableView.dequeueReusableCellWithIdentifier( ReusedCellID, forIndexPath: indexPath)

   這樣,就可以防止cell無限的被建立,重用cell

  

二、cell的圖片非同步載入

當設定圖片非同步載入之後,cell 的圖片就放在了子線程中執行,但是,如果此時下滑,也會出現卡頓的現象

解決辦法:

  非同步下載,在以下兩個方法中下載圖片、  

// 當結束拖拽的時候- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate// 減速的時候- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

 

具體操作:  

//擷取可見部分的對象NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];for (NSIndexPath *indexPath in visiblePaths){  //擷取的dataSource裡面的對象,並且判斷載入完成的不需要再次非同步載入  <code>}同時在cell繪製中也做限制- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{  if (self.tableView.dragging == NO && self.tableView.decelerating == NO) { //開始非同步載入圖片 <code> }
}

     
三、盡量少用透明的圖層

  內部的渲染機制

 

【IOS開發】UITableView的效能最佳化

聯繫我們

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