UITableView 專題 2 —— 數(shù)據(jù)源基本使用

UITableView 的數(shù)據(jù)顯示主要牽扯到的就是一個(gè)數(shù)據(jù)源方法的使用。
基本思路是:

  1. 數(shù)據(jù)源的設(shè)置
  2. 數(shù)據(jù)源方法的實(shí)現(xiàn)
  3. tableView 對數(shù)據(jù)源的三次詢問
    3.1 tableView 有多少組
    3.2 tableView 每組有多少行
    3.3 tableView 每行顯示什么內(nèi)容
  //*******************  這三個(gè)方法是 tableView 顯示數(shù)據(jù)的基本方法蟋字,實(shí)現(xiàn)這三個(gè)方法就可以顯示 tableView 的內(nèi)容  ************************
    @available(iOS 2.0, *)  // iOS 2.O 開始使用
    //  tableView 有多少行
    public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

    // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
    // 行顯示的時(shí)候呛占,通過設(shè)置 cell 的重用標(biāo)識符挪拟,和調(diào)用 dequeueReusableCellWithIdentifier 方法來確認(rèn)是否有可重用的 cell歧斟,嘗試去重用 cell肴茄,
    // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

    @available(iOS 2.0, *)
    // 每一行 cell 顯示什么內(nèi)容
    public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

    @available(iOS 2.0, *)
    // tableView 有多少組 ( 這個(gè)方法是可選的 默認(rèn)的實(shí)現(xiàn)返回時(shí) 1 晌畅,不實(shí)現(xiàn)這個(gè)數(shù)據(jù)源方法,tableView 就顯示一組數(shù)據(jù))
    optional public func numberOfSectionsInTableView(tableView: UITableView) -> Int // Default is 1 if not implemented

1独郎、tableView 數(shù)據(jù)顯示的基本設(shè)置

簡單數(shù)據(jù)展示的實(shí)例

只是展示了一個(gè)單行的文本數(shù)據(jù)

//
//  ViewController.swift
//  UITableViewTest
//
//  Created by 肖卓鴻 on 16/6/2.
//  Copyright ? 2016年 肖卓鴻. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var tableView: UITableView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView = UITableView()
        
        // 1. 設(shè)置數(shù)據(jù)源代理
        tableView?.dataSource = self
        view.addSubview(tableView!)
    }
    
    
    // 2. 這里進(jìn)行 tableView 的 frame 設(shè)置主要是屏幕的適配
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        tableView?.frame = view.frame
    }
}

// 實(shí)現(xiàn)數(shù)據(jù)源
// MARK: - UITableViewDataSource
/*
 使用擴(kuò)展來實(shí)現(xiàn)數(shù)據(jù)源踩麦,這樣會(huì)使代碼結(jié)構(gòu)看起來更加清晰枚赡。
 */
extension ViewController : UITableViewDataSource {
    
    // 3.1 tableView 有幾組
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }
    
     // 3.2 tableView 每組有幾行
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    // 3.3 tableView 每行顯示什么內(nèi)容
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        // 創(chuàng)建 cell (cell 沒有使用重用, 此處只是為了展示基本使用)
        let cell = UITableViewCell()
        
        // cell 文本數(shù)據(jù)的顯示內(nèi)容設(shè)置
        cell.textLabel?.text = "\(indexPath.section)組" + "-" + "\(indexPath.row)行"
        return cell
    }
}

效果圖:

Snip20160602_26.png

數(shù)據(jù)源方法調(diào)用的追蹤
在三個(gè)數(shù)據(jù)源方法中添加

print(#function)
Snip20160602_27.png
numberOfSectionsInTableView
tableView(_:numberOfRowsInSection:)
tableView(_:numberOfRowsInSection:)
numberOfSectionsInTableView
tableView(_:numberOfRowsInSection:)
tableView(_:numberOfRowsInSection:)
numberOfSectionsInTableView
tableView(_:numberOfRowsInSection:)
tableView(_:numberOfRowsInSection:)
numberOfSectionsInTableView
tableView(_:numberOfRowsInSection:)
tableView(_:numberOfRowsInSection:)
tableView(_:cellForRowAtIndexPath:)
tableView(_:cellForRowAtIndexPath:)
tableView(_:cellForRowAtIndexPath:)
tableView(_:cellForRowAtIndexPath:)
tableView(_:cellForRowAtIndexPath:)
tableView(_:cellForRowAtIndexPath:)
tableView(_:cellForRowAtIndexPath:)
tableView(_:cellForRowAtIndexPath:)
tableView(_:cellForRowAtIndexPath:)
tableView(_:cellForRowAtIndexPath:)
  1. 方法的調(diào)用順序是
    numberOfSectionsInTableView
    tableView(:numberOfRowsInSection:)
    tableView(
    :cellForRowAtIndexPath:)

2氓癌、cell 視圖的重用

為了提高 tableView 的性能,我們對 cell 進(jìn)行重用贫橙。
使用重用之后贪婉,第一次加載 tableView 的時(shí)候就只會(huì)創(chuàng)建界面可以顯示的幾個(gè) cell,當(dāng)滾動(dòng) tableView 的時(shí)候卢肃,會(huì)從緩存池中獲取 cell 不會(huì)創(chuàng)建 cell 疲迂。當(dāng) cell 消失的時(shí)候?qū)?cell 放入緩存池中。
在上面的數(shù)據(jù)源方法中返回 10000

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100000
}

這樣就可以感覺到明顯的卡頓莫湘。而且內(nèi)存也會(huì)暴漲尤蒿。(在 cell 上設(shè)置圖片后效果會(huì)比較明顯)

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        // 創(chuàng)建 cell (cell 沒有使用重用, 此處只是為了展示基本使用)
        let cell = UITableViewCell()

       print(#function)
        return cell
}

會(huì)打印 100000 次
使用重用后,界面可以顯示幾個(gè) cell 就打印幾次

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // 老程序員會(huì)這么寫 cell 的重用
        var cell = tableView.dequeueReusableCellWithIdentifier("cell")
        if cell == nil {
            cell = UITableViewCell(style: .Default, reuseIdentifier: "cell")
        }
        print(#function)
        return cell!
    }

相比之下性能會(huì)高很多

上面是老式的 cell 的重用寫法
cell 重用的使用步驟

  1. 注冊 cell
  2. 數(shù)據(jù)源方法中獲取 cell
  3. 設(shè)置 cell 的數(shù)據(jù)后 在數(shù)據(jù)源方法中返回 cell

cell 的注冊:

// cell 的 nib 方式注冊幅垮,
public func registerNib(nib: UINib?, forCellReuseIdentifier identifier: String)
// cell 的 class 方式注冊
public func registerClass(cellClass: AnyClass?, forCellReuseIdentifier identifier: String)

// header 和 footer nib 方式注冊
public func registerNib(nib: UINib?, forHeaderFooterViewReuseIdentifier identifier: String)
// header 和 footer class 方式注冊
public func registerClass(aClass: AnyClass?, forHeaderFooterViewReuseIdentifier identifier: String)

// 注冊代碼
tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: UITableViewCell.identifier)
tableView.registerNib(UITableViewCell.nib, forCellReuseIdentifier: UITableViewCell.identifier)

// UITableViewCell.nib 和 UITableViewCell.identifier 的說明
import UIKit

extension UITableViewCell {
    
    /*!
     cell 重用標(biāo)識符 (標(biāo)識符默認(rèn)就是 class 的字符串)
     */
    class var identifier:String  {
        return "\(self.classForCoder())"
    }
    
    /*!
     cell 的 nib 文件
     */
    class var nib:UINib {
        return UINib(nibName: "\(self.classForCoder())", bundle: nil)
    }
}

 使用 UITableViewCell.nib 和 UITableViewCell.identifier 這種方式可以避免直接寫字符串容易寫錯(cuò)的麻煩
注意點(diǎn): 使用 nib 方式的時(shí)候腰池,xib 的文件名稱和 視圖類的類名一定要相同。

緩存池中 cell 的獲取

public func dequeueReusableCellWithIdentifier(identifier: String) -> UITableViewCell? // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.

// 使用這個(gè)方法 cell 必須是注冊的(這是和上面方法的最大區(qū)別)
public func dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath) -> UITableViewCell


// cell 從緩存池中獲取的代碼
// 不進(jìn)行注冊忙芒,就使用舊的寫法就可以了
let cell = tableView.dequeueReusableCellWithIdentifier(UITableViewCell.identifier)
// 使用這個(gè)方法 cell 必須提前注冊
let cell = tableView.dequeueReusableCellWithIdentifier(UITableViewCell.identifier, forIndexPath: indexPath)

// cell 數(shù)據(jù)的設(shè)置 和 cell 返回
cell?.textLabel?.text = "hello world"        
return cell!
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末示弓,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子呵萨,更是在濱河造成了極大的恐慌奏属,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件潮峦,死亡現(xiàn)場離奇詭異囱皿,居然都是意外死亡勇婴,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進(jìn)店門嘱腥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來咆耿,“玉大人,你說我怎么就攤上這事爹橱∪荩” “怎么了?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵愧驱,是天一觀的道長慰技。 經(jīng)常有香客問我,道長组砚,這世上最難降的妖魔是什么吻商? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮糟红,結(jié)果婚禮上艾帐,老公的妹妹穿的比我還像新娘。我一直安慰自己盆偿,他們只是感情好柒爸,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著事扭,像睡著了一般捎稚。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上求橄,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天今野,我揣著相機(jī)與錄音,去河邊找鬼罐农。 笑死条霜,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的涵亏。 我是一名探鬼主播宰睡,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼溯乒!你這毒婦竟也來了夹厌?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤裆悄,失蹤者是張志新(化名)和其女友劉穎矛纹,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體光稼,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡或南,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年孩等,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片采够。...
    茶點(diǎn)故事閱讀 39,764評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡肄方,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蹬癌,到底是詐尸還是另有隱情权她,我是刑警寧澤,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布逝薪,位于F島的核電站隅要,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏董济。R本人自食惡果不足惜步清,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望虏肾。 院中可真熱鬧廓啊,春花似錦、人聲如沸封豪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽撑毛。三九已至书聚,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間藻雌,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工斩个, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留胯杭,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓受啥,卻偏偏與公主長得像做个,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子滚局,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,665評論 2 354

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