一挫酿、概述
UITableView是iOS開發(fā)比不可少也是最重要的一個(gè)控件類斜筐。可以說任何一個(gè)做iOS開發(fā)的人都必須熟練使用和掌握它必指。本文主要就是提供一個(gè)學(xué)習(xí)使用TableView的指南括饶。
要說UITableView必須要介紹他的幾個(gè)親戚:UITableViewDelegate株茶,UITableViewDataSource,UITableViewCell图焰。其中前兩個(gè)是TableView遵守的兩個(gè)protocol(別告訴我你不知道啥叫protocol哦)启盛。然后本文會(huì)再列出TableView最常用最重要的一些知識(shí)點(diǎn)。最后再介紹幾個(gè)參考例子技羔。
二僵闯、UITableView和它的親戚們
1. UITableView
參考:
https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html
1) 初始化 UITableView對(duì)象
– initWithFrame:style:? // 代碼生成方式,如果你在nib里加的tableview不需要使用這個(gè)方法
2)配置TableView
– dequeueReusableCellWithIdentifier: // 必須要實(shí)現(xiàn)的方法藤滥,與TableView同生同死
style? property // 有兩種 UITableViewStylePlain, UITableViewStyleGrouped鳖粟,經(jīng)常用
– numberOfRowsInSection:? //一個(gè)section有多少行,經(jīng)常用
– numberOfSections? //一個(gè)TableView有多少個(gè)section拙绊,經(jīng)常用
rowHeight? property // 行高向图,和tableView:heightForRowAtIndexPath:有性能上的區(qū)別
separatorStyle? property // cell之間的分割線?待確認(rèn)
separatorColor? property // 同上
backgroundView? property // tableview的背景view, 這個(gè)背景view在所有cell, header views, footer views之后
tableHeaderView? property // tableview上方的一個(gè)headerView, 和delete里的section header不是一個(gè)概念
tableFooterView? property // tableview下方的一個(gè)footerview
sectionHeaderHeight? property // section Header的高度时呀,
sectionFooterHeight? property // sectjion Footer的高度
sectionIndexMinimumDisplayRowCount? property //? 功能待確認(rèn)张漂? 參考例子:? TheElements
3) 訪問Cells和Sections
– cellForRowAtIndexPath: //根據(jù)IndexPath返回cell
– indexPathForCell: //根據(jù)cell返回它的indexPath,和上面的方法互補(bǔ)
– indexPathForRowAtPoint://根據(jù)一個(gè)幾何點(diǎn)返回indexPath,如果超過邊界返回nil
– indexPathsForRowsInRect: //根據(jù)一個(gè)幾何的矩形返回矩形所覆蓋的行,返回是一個(gè)indexPath數(shù)組
– visibleCells // 不清楚怎么用,待確認(rèn)
– indexPathsForVisibleRows //同上
4) 滾動(dòng)TableView
– scrollToRowAtIndexPath:atScrollPosition:animated: // 滾動(dòng)到指定位置
– scrollToNearestSelectedRowAtScrollPosition:animated: // 同上
5) 管理sections
– indexPathForSelectedRow //返回選定行的indexPath,單行
– indexPathsForSelectedRows //返回選定行的indexPath數(shù)組谨娜,多行
– selectRowAtIndexPath:animated:scrollPosition: //根據(jù)indexPath選擇一行
– deselectRowAtIndexPath:animated: //反選一行,有何用磺陡?
allowsSelection? property //是否允許用戶選取一行
allowsMultipleSelection? property // 是否選取多行趴梢,缺省為NO. 可以試試YES后的效果漠畜,哈哈
allowsSelectionDuringEditing? property // 編輯模式時(shí)是否可選取一行
allowsMultipleSelectionDuringEditing? property // 編輯模式時(shí)可否選取多行
6) 插入、刪除坞靶、移動(dòng)行和sections
– beginUpdates // 和endUpdates一起用憔狞,讓插入、刪除彰阴、選擇操作同時(shí)動(dòng)畫瘾敢,沒用過
– endUpdates //
– insertRowsAtIndexPaths:withRowAnimation: //根據(jù)indexPath數(shù)組插入行
– deleteRowsAtIndexPaths:withRowAnimation: //根據(jù)indexPath數(shù)組刪除行
– moveRowAtIndexPath:toIndexPath: //移動(dòng)一行到另一行
– insertSections:withRowAnimation: //插入sections
– deleteSections:withRowAnimation: //刪除sections
– moveSection:toSection: //移動(dòng)section
7) 管理和編輯cell
editing? property // YES進(jìn)入編輯模式,tableview cell會(huì)出現(xiàn)插入尿这、刪除簇抵、重排序的控件
– setEditing:animated: //設(shè)置進(jìn)入退出編輯模式
8) 重新加載TableView
– reloadData // 重建整個(gè)表,包括cells射众、header碟摆、footer,indexs
– reloadRowsAtIndexPaths:withRowAnimation: // 改進(jìn)叨橱,不用reload整個(gè)表
– reloadSections:withRowAnimation: // 同上
– reloadSectionIndexTitles // 同上
9) 訪問TableView的畫圖區(qū)
– rectForSection: // 返回指定section的矩形
– rectForRowAtIndexPath: //返回indexPath指定行的矩形
– rectForFooterInSection: // 返回section的footer矩形
– rectForHeaderInSection: // 返回section的header矩形
10) Registering Nib Objects for Cell Reuse
– registerNib:forCellReuseIdentifier: //
11) 管理委托和數(shù)據(jù)源 (重要)
dataSource? property // 通常會(huì)這么用: myTableView.delegate = self; self 為viewController
delegate? property // 通常會(huì)這么用:???? myTableView.dataSource = self; self 為viewController