談到復(fù)用狰贯,首先需要了解兩個代理UITableViewDelegate
劫瞳,UITableViewDataSource
呕童。四個方法:
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)
這幾個方法分別設(shè)置了一組中cell的個數(shù)崔赌、cell的內(nèi)容意蛀、cell的高、選中cell的結(jié)果健芭。
只有在cell被滑動出界面的時候县钥,此cell才會被加入到復(fù)用隊列中。每次在創(chuàng)建cell的時候慈迈,程序會首先通過調(diào)用dequeueReusableCellWithIdentifier("cellType")方法若贮,到復(fù)用隊列中去尋找標(biāo)示符為“cellType”的cell,如果找不到痒留,返回nil谴麦,然后程序去通過調(diào)用
cell = UITableViewCell.init(style: .Subtitle, reuseIdentifier: "cellType")
來創(chuàng)建標(biāo)示符為“cellType”的cell。