【iOS開發(fā)】UITableView中dequeueReusableCell帶indexpath方法和不帶的區(qū)別

官方的描述:

    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;
    }
    
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市显沈,隨后出現(xiàn)的幾起案子软瞎,更是在濱河造成了極大的恐慌逢唤,老刑警劉巖拉讯,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異鳖藕,居然都是意外死亡魔慷,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門著恩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來院尔,“玉大人,你說我怎么就攤上這事喉誊⊙冢” “怎么了?”我有些...
    開封第一講書人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵伍茄,是天一觀的道長(zhǎng)栋盹。 經(jīng)常有香客問我,道長(zhǎng)敷矫,這世上最難降的妖魔是什么例获? 我笑而不...
    開封第一講書人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮曹仗,結(jié)果婚禮上榨汤,老公的妹妹穿的比我還像新娘。我一直安慰自己怎茫,他們只是感情好收壕,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般蜜宪。 火紅的嫁衣襯著肌膚如雪旬渠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,156評(píng)論 1 308
  • 那天端壳,我揣著相機(jī)與錄音告丢,去河邊找鬼。 笑死损谦,一個(gè)胖子當(dāng)著我的面吹牛岖免,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播照捡,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼颅湘,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了栗精?” 一聲冷哼從身側(cè)響起闯参,我...
    開封第一講書人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎悲立,沒想到半個(gè)月后鹿寨,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡薪夕,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年脚草,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片原献。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡馏慨,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出姑隅,到底是詐尸還是另有隱情写隶,我是刑警寧澤,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布讲仰,位于F島的核電站慕趴,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏叮盘。R本人自食惡果不足惜秩贰,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望柔吼。 院中可真熱鬧毒费,春花似錦、人聲如沸愈魏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至溪厘,卻和暖如春胡本,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背畸悬。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來泰國打工侧甫, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蹋宦。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓披粟,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親冷冗。 傳聞我的和親對(duì)象是個(gè)殘疾皇子守屉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容