UITableView的復用機制

談到復用,首先需要了解兩個代理UITableViewDelegateUITableViewDataSource。四個方法:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

這幾個方法分別設置了一組中cell的個數(shù)、cell的內(nèi)容、cell的高、選中cell的結(jié)果。
只有在cell被滑動出界面的時候,此cell才會被加入到復用隊列中。每次在創(chuàng)建cell的時候,程序會首先通過調(diào)用dequeueReusableCellWithIdentifier("cellType")方法,到復用隊列中去尋找標示符為“cellType”的cell,如果找不到,返回nil,然后程序去通過調(diào)用

    cell = UITableViewCell.init(style: .Subtitle, reuseIdentifier: "cellType")

來創(chuàng)建標示符為“cellType”的cell。

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

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

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