UITableView

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
UITableView
let tableView = UITableView.init(frame: CGRect(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: SCREEN_WIDTH, height: SCREEN_HEIGHT)), style: .plain)
    
tableView.delegate = self
    
tableView.dataSource = self
    
tableView.tableFooterView = UIView.init()
    
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "mycell")

self.view.addSubview(tableView)
UITableViewDelegate
// Cell 內(nèi)容
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    var  cell = tableView.dequeueReusableCell(withIdentifier: "mycell")
    
    if cell == nil {
        
        cell = UITableViewCell(style: .default, reuseIdentifier: "mycell")
    }
    
    cell?.textLabel?.text = dataSource[indexPath.row]
    
    return cell!
}

// 自定義 頭視圖
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
    let headerView = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 44))
    
    headerView.backgroundColor = UIColor.red
    
    return headerView
}

// 自定義 尾視圖
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    
    let footerView = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 100))
    
    footerView.backgroundColor = UIColor.green
    
    return footerView
}

// cell 將要顯示
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    
}

// cell 已經(jīng)顯示
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    
}

// header 將要顯示
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    
}

// header 已經(jīng)顯示
func tableView(_ tableView: UITableView, didEndDisplayingHeaderView view: UIView, forSection section: Int) {
    
}

// footer 將要顯示
func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
    
}

// footer 已經(jīng)顯示
func tableView(_ tableView: UITableView, didEndDisplayingFooterView view: UIView, forSection section: Int) {
    
}

// 設(shè)置cell 高
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
    return 44
}

// 設(shè)置cell 高的估計(jì)值
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    
    return 100.0
}

// 設(shè)置header 高
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    
    return 44.0
}

// 設(shè)置header 高的估計(jì)值
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
    
    return 100.0
}

// 設(shè)置footer 高
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    
    return 44.0
}

// 設(shè)置footer 高的估計(jì)值
func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
    
    return 100.0
}

// 設(shè)置cell 是否可以高亮
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
    
    return true
}

// cell 高亮?xí)r調(diào)用
func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
    
}

// cell 取消高亮?xí)r調(diào)用
func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
    
}

// cell 將要選中
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    
    return indexPath
}

// cell 已經(jīng)選中
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    tableView.deselectRow(at: indexPath, animated: true)
}

// cell 將要取消選中
func tableView(_ tableView: UITableView, willDeselectRowAt indexPath: IndexPath) -> IndexPath? {
    
    return indexPath
}

// cell 已經(jīng)取消選中
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    
}

// 設(shè)置被編輯時(shí)的樣式(默認(rèn)刪除)
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    
    return .delete
}

// 自定義刪除按鈕
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
    
    return "一鍵刪除"
}

// 設(shè)置編輯時(shí)按鈕
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    
    let action1 = UITableViewRowAction.init(style: .normal, title: "喜歡") { (action:UITableViewRowAction, indexPath:IndexPath) in
        
        //邏輯
    }
    
    let action2 = UITableViewRowAction.init(style: .normal, title: "不喜歡") { (action:UITableViewRowAction, indexPath:IndexPath) in
        
        //邏輯
    }
    
    return [action1, action2]
}

// 編輯時(shí)背景是否縮進(jìn)
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
    
    return true
}

// 將要編輯
func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
    
}

// 結(jié)束結(jié)束編輯
func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
    
}

// 移動(dòng)行
func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
    
    return proposedDestinationIndexPath
}
UITableViewDataSource
// 返回每個(gè)分組行數(shù)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    return dataSource.count
}

// 返回分組數(shù)(默認(rèn)為1)
func numberOfSections(in tableView: UITableView) -> Int {
    
    return 1
}

// 返回每個(gè)分區(qū)頭部標(biāo)題
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    
    return "頭"
}

// 返回每個(gè)分區(qū)尾部標(biāo)題
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    
    return "尾"
}

// 行是否可編輯
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    
    return true
}

// 編輯調(diào)用
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    
}

// 行是否可移動(dòng)
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    
    return true
}

// 移動(dòng)調(diào)用
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    
}

// 索引
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
    
    return ["0","1","2","3","4","5","6","7","8","9"]
}

// 設(shè)置索引對(duì)應(yīng)的分區(qū)
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
    
    return 0
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末鸠珠,一起剝皮案震驚了整個(gè)濱河市溜歪,隨后出現(xiàn)的幾起案子辕羽,更是在濱河造成了極大的恐慌拓萌,老刑警劉巖耍休,帶你破解...
    沈念sama閱讀 218,941評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蛤迎,死亡現(xiàn)場(chǎng)離奇詭異舔清,居然都是意外死亡徐许,警方通過(guò)查閱死者的電腦和手機(jī)施蜜,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)雌隅,“玉大人翻默,你說(shuō)我怎么就攤上這事∏∑穑” “怎么了修械?”我有些...
    開封第一講書人閱讀 165,345評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)检盼。 經(jīng)常有香客問(wèn)我肯污,道長(zhǎng),這世上最難降的妖魔是什么吨枉? 我笑而不...
    開封第一講書人閱讀 58,851評(píng)論 1 295
  • 正文 為了忘掉前任蹦渣,我火速辦了婚禮,結(jié)果婚禮上貌亭,老公的妹妹穿的比我還像新娘柬唯。我一直安慰自己,他們只是感情好圃庭,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評(píng)論 6 392
  • 文/花漫 我一把揭開白布锄奢。 她就那樣靜靜地躺著,像睡著了一般剧腻。 火紅的嫁衣襯著肌膚如雪拘央。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,688評(píng)論 1 305
  • 那天恕酸,我揣著相機(jī)與錄音堪滨,去河邊找鬼。 笑死蕊温,一個(gè)胖子當(dāng)著我的面吹牛袱箱,可吹牛的內(nèi)容都是我干的遏乔。 我是一名探鬼主播,決...
    沈念sama閱讀 40,414評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼发笔,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼盟萨!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起了讨,我...
    開封第一講書人閱讀 39,319評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤捻激,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后前计,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體胞谭,經(jīng)...
    沈念sama閱讀 45,775評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年男杈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了丈屹。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,096評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡伶棒,死狀恐怖旺垒,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情肤无,我是刑警寧澤先蒋,帶...
    沈念sama閱讀 35,789評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站宛渐,受9級(jí)特大地震影響竞漾,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜皇忿,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評(píng)論 3 331
  • 文/蒙蒙 一畴蹭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鳍烁,春花似錦叨襟、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至爹梁,卻和暖如春右犹,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背姚垃。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工念链, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,308評(píng)論 3 372
  • 正文 我出身青樓掂墓,卻偏偏與公主長(zhǎng)得像谦纱,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子君编,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評(píng)論 2 355

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