2014-09-03 46 views
0
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat { 
     let cellPrototype: NDCustomTableViewCell = NDCustomTableViewCell() 
    return cellPrototype.frame.size.height; 
} 

let cellPrototype是一個常量,這隻會執行一個嗎?不是函數heightForRow,而是細胞原型的初始化。這是Swift中原型設計模式的正確使用嗎?

回答

2

每次調用該函數時都會生成一個新單元。在其範圍內(兩行),cellPrototype將不允許更改(儘管單元本身可以更改)。

你想要的方法是dequeueReuseableCellWithIdentifier()。正確的單元配置是一個有點涉及的主題,所以你應該閱讀Table View Programming Guide瞭解完整的細節。


編輯:關於這個多一點,另一種方式思考來處理這個(而不是dequeueReuseableCellWithIdentifier)是剛剛創建包含用於查詢原型細胞的特性。

+0

哦,我們沒有想到的事情!謝謝 :) – nmdias 2014-09-03 20:08:37

相關問題