談到復用,首先需要了解兩個代理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)
這幾個方法分別設置了一組中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。