我的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.
所以我想實現我的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。 (和許多其他功能)
我的頭包含了一些項目,如
- 標籤來顯示標題,日期,時間從火力
- 圖像下載,可以是可選的
- 圖像描述
- 像btn,commentbtn,更多,btn
所有這些函數都在我的TableSectionHeader.swi從UITableViewHeaderFooterView繼承的英尺
你能解釋一下你的意思嗎?「它看起來很像你試圖在標題中存儲狀態 - 你應該將狀態存儲在tableView之外。」?
我在此取消Alamofire請求的原因是因爲該單元格已取出。所以如果用戶滾動得非常快,那麼這個單元格會得到很多alamofire請求。所以我先取消了它,然後重新打開一個下載請求(在cell內。configureCell)如果我沒有任何東西在我的緩存
我不知道如何打印章節可以幫助識別。我認爲這是一個錯誤的想法,我在這裏把所有東西放在willDisplayHeaderView代碼中(因爲大多數情況下你會把它放在viewForHeaderInSection中)。或者,也許它只是語法
我剛纔試過了,因爲某些原因,演員總是失敗? – user172902