官方的描述:
open func dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell? // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
@available(iOS 6.0, *)
open func dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
iOS6.0之后新增的func dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell
兩種重用機(jī)制的區(qū)別
1蜒秤、方法open func dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell?
cell最初從cell隊(duì)列中獲取莉测,可能為空,需要判斷是否空處理鞠绰。
使用實(shí)例:
import UIKit
class ListTableViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource{
private let identifer = "ListTableViewControlleritemcell"
private var listTableView:UITableView? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
createTableView();
}
//MARK:- 創(chuàng)建tableview
func createTableView() -> Void {
listTableView = UITableView.init();
listTableView?.delegate = self;
listTableView?.dataSource = self;
self.view.addSubview(listTableView!);
}
//MARK:- tableview dataSourceDelegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10;
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//獲取重用cell 有可能獲取不到為nil
var cell = tableView.dequeueReusableCell(withIdentifier: identifer);
//判斷為nil 初始化一個(gè)新的cell
if(cell == nil)
{
cell = UITableViewCell.init(style: .default, reuseIdentifier: identifer);
}
cell?.textLabel?.text = "描述";
return cell!;
}
}
2困檩、方法open func dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell
在初始UITableView的時(shí)候祠挫,注冊(cè)cell,系統(tǒng)會(huì)自動(dòng)初始化。
如果未使用register方法就會(huì)報(bào)錯(cuò)
使用實(shí)例:
import UIKit
class ListTableViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource{
private let identifer = "ListTableViewControlleritemcell"
private var listTableView:UITableView? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
createTableView();
}
//MARK:- 創(chuàng)建tableview
func createTableView() -> Void {
listTableView = UITableView.init();
listTableView?.delegate = self;
listTableView?.dataSource = self;
self.view.addSubview(listTableView!);
//1悼沿、注冊(cè)cell
listTableView?.register(UITableViewCell.self, forCellReuseIdentifier: identifer)
}
//MARK:- tableview dataSourceDelegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10;
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//2等舔、獲取重用的cell
let cell = tableView.dequeueReusableCell(withIdentifier: identifer,for: indexPath);
cell.textLabel?.text = "描述"
return cell;
}
}