2016-09-07 64 views
0

我的tableview不能平滑滾動。我看到了蘋果的這個評論。通過將代碼移動到willDisplayHeaderView來改善TableView滾動

But very important thing is still there: tableView:cellForRowAtIndexPath: method, which should be implemented in the dataSource of UITableView, called for each cell and should work fast. So you must return reused cell instance as quickly as possible.

Don’t perform data binding at this point, because there’s no cell on screen yet. For this you can use tableView:willDisplayCell:forRowAtIndexPath: method which can be implemented in the delegate of UITableView. The method called exactly before showing cell in UITableView’s bounds.

Perfect smooth scrolling

所以我想實現我的viewForHeaderInSection所有代碼willDisplayHeaderView(注意,我用的部分,而不是行的這個具體的例子,因爲我有自定義的部分)。但是,我得到一個「致命的錯誤:意外發現零而展開的可選值」在

let cell = tableView.headerViewForSection(section) as! TableSectionHeader 

下面是我墜毀

原件(注原件和嘗試的代碼錯誤,此代碼工作正常我試圖改善一些小的滾動滯後問題)

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    if let cell = tableView.dequeueReusableHeaderFooterViewWithIdentifier("TableSectionHeader") as? TableSectionHeader { 

     // Cancel request of current cell if there is a request going on to prevent requesnt info from background from previous use of same cell 
     cell.AlamoFireRequest?.cancel() 

     var image: UIImage? 
     if let url = post.imageUrl { 
      image = DiscoverVC.imageCache.objectForKey(url) as? UIImage 
     } 

     cell.configureCell(post) // This is data binding part 
     cell.delegate = self 

     return cell 

    } else { 
     return TableSectionHeader() 
    } 
} 

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 

} 

嘗試

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    if let cell = tableView.dequeueReusableHeaderFooterViewWithIdentifier("TableSectionHeader") as? TableSectionHeader { 
     return cell 

    } else { 
     return TableSectionHeader() 
    } 
} 

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 

    let cell = tableView.headerViewForSection(section) as! TableSectionHeader 

    // Cancel request of current cell if there is a request going on to prevent requesnt info from background from previous use of same cell 
    cell.AlamoFireRequest?.cancel() 

    var image: UIImage? 
    if let url = post.imageUrl { 
     image = DiscoverVC.imageCache.objectForKey(url) as? UIImage 
    } 

    cell.configureCell(post) // This is data binding part 
    cell.delegate = self 
} 

-----更新,以解決邁克爾的答案------

由於有上回復字數限制,這裏是從邁克爾

應答的響應

你是正確的,我已經更新在那裏我從我的問題中得到了一些片段。我的錯誤

我同意這個問題很可能會在其他地方說謊,但這是我目前正在經歷的事情。我的桌面視圖可以滾動,但有時候有圖像會減慢一點。所以我正在通過一些步驟來消除任何潛在的原因。

我特別沒有使用的原因,如果讓這裏是因爲我期待單元格被顯示將TableSectionHeader。我試圖添加它,然後我總是得到失敗的演員。

我分類UITableViewHeaderFooterView的原因是因爲我的headerview是一個Xib文件,我有func configureCell(),所以我可以調用cell.configureCell。 (和許多其他功能)

我的頭包含了一些項目,如

  1. 標籤來顯示標題,日期,時間從火力
  2. 圖像下載,可以是可選的
  3. 圖像描述
  4. 像btn,commentbtn,更多,btn

所有這些函數都在我的TableSectionHeader.swi從UITableViewHeaderFooterView繼承的英尺

你能解釋一下你的意思嗎?「它看起來很像你試圖在標題中存儲狀態 - 你應該將狀態存儲在tableView之外。」?

我在此取消Alamofire請求的原因是因爲該單元格已取出。所以如果用戶滾動得非常快,那麼這個單元格會得到很多alamofire請求。所以我先取消了它,然後重新打開一個下載請求(在cell內。configureCell)如果我沒有任何東西在我的緩存

我不知道如何打印章節可以幫助識別。我認爲這是一個錯誤的想法,我在這裏把所有東西放在willDisplayHeaderView代碼中(因爲大多數情況下你會把它放在viewForHeaderInSection中)。或者,也許它只是語法

回答

0

你在方法中給了標題視圖,你只需要施放它。試試這個:

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 

    guard let cell = view as? TableSectionHeader else { return } 

    cell.AlamoFireRequest?.cancel() 

    ... 
} 
+0

我剛纔試過了,因爲某些原因,演員總是失敗? – user172902

0

我不認爲評論來自蘋果,我認爲你的問題可能出在其它地方(例如,爲要在後臺運行一個任務阻塞主隊列)。如果您發佈TableSectionHeader的內容,這可能會變得更清晰。

按就不管,你在下面一行得到一個零值異常:

let cell = tableView.headerViewForSection(section) as! TableSectionHeader 

這是因爲你強迫塑像作爲TableSectionHeader,這無疑是一個也沒有。如果將其更改爲if letguard let,您將看到不同的代碼路徑。

既然你顯然希望它永遠是那種類型的(畢竟,這是你在建立viewForHeaderInSection什麼),有事情發生,你不知道(爲什麼你繼承UITableViewHeaderFooterView呢?)。我傾向於在兩部分中打印節號和視圖,以便了解真正發生的情況。它看起來很像你試圖在頭文件中存儲狀態 - 你應該在tableView之外存儲狀態。

此外,應該不需要取消Alamofire請求,因爲這不是性能問題的原因 - 檢索圖像應該只是觸發另一個請求。否則,當用戶滾動時,不會顯示任何圖像,因爲請求將不斷取消。他們必須獨自離開屏幕,等待這些圖像在其他地方滾動之前加載。

+0

嗨邁克爾,請查看我在這裏由於字數限制問題的回覆。 – user172902