UITableView的方法總結

1.創(chuàng)建一個UITableView對象

ITableView *tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];

2.separatorColor?分割線顏色

?ableView.separatorColor = [UIColor redColor];

3.rowHeight?調整每個cell點高度(默認44)

?tableView.rowHeight = 60;

4.reloadData?刷新數(shù)據(jù)

[tableView reloadData];

5.協(xié)議兩個必須實現(xiàn)的方法

? ?控制一個section中cell的多少

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

? ?控制cell中的內容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)

indexPath

6.選中cell時候使用的方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

7.取消選中時候用的方法(不常用)

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

8.控制分區(qū)個數(shù)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

9.section上Header顯示的內容

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

10.section上Footer顯示的內容

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

11.section頂部的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

12.cell的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

13 該方法返回值用于在表格右邊建立一個浮動的索引

- (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView;

cell相關:

1.返回表格中指定indexPath對應的cell

- (UITableViewCell*)cellForRowAtIndexPath:(NSIndexPath*)indexPath;

2.返回指定cell的indexPath

- (NSIndexPath*)indexPathForCell:(UITableViewCell*)cell;

3.返回表格中指定點所在的indexPath

- (NSIndexPath*)indexPathForRowAtPoint:(CGPoint)point;

4.返回表格中指定區(qū)域內所有indexPath 組成的數(shù)組

- (NSArray*)indexPathsForRowsInRect:(CGRect)rect;

5.返回表格中所有可見區(qū)域內cell的數(shù)組

- (NSArray*)visibleCells;

6.返回表格中所有可見區(qū)域內cell對應indexPath所組成的數(shù)組

- (NSArray*)indexPathsForVisibleRows;

7.控制該表格滾動到指定indexPath對應的cell的頂端 中間 或者下方

- (void)scrollToRowAtIndexPath:(NSIndexPath*)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

8.控制該表格滾動到選中cell的頂端 中間 或者下方

-(void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

處理單元格的選中

1.控制該表格是否允許被選中

@property(nonatomic)BOOLallowsSelection

2.控制該表格是否允許多選

@property(nonatomic)BOOLallowsMultipleSelection

3.控制表格處于編輯狀態(tài)時是否允許被選中

@property(nonatomic)BOOLallowsSelectionDuringEditing;

4.控制表格處于編輯狀態(tài)時是否允許被多選

@property(nonatomic)BOOLallowsMultipleSelectionDuringEditing

5.獲取選中cell對應的indexPath

- (NSIndexPath*)indexPathForSelectedRow;

6.獲取所有被選中的cell對應的indexPath組成的數(shù)組

- (NSArray*)indexPathsForSelectedRows

7.控制該表格選中指定indexPath對應的表格行,最后一個參數(shù)控制是否滾動到被選中行的頂端 中間 和底部

- (void)selectRowAtIndexPath:(NSIndexPath*)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;

8.控制取消選中該表格中指定indexPath對應的表格行

- (void)deselectRowAtIndexPath:(NSIndexPath*)indexPath animated:(BOOL)animated;

9.當用戶將要選中表格中的某行時觸發(fā)方法

- (NSIndexPath*)tableView:(UITableView*)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath;

10.當用戶完成選中表格中的某行時觸發(fā)方法

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath

11.當用戶將要取消選中表格中某行時觸發(fā)

- (NSIndexPath*)tableView:(UITableView*)tableView willDeselectRowAtIndexPath:(NSIndexPath*)indexPath

12.當用戶完成取消選中表格中某行時觸發(fā)

- (void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath

關于對表格的編輯

1.對表格控件執(zhí)行多個連續(xù)的插入,刪除和移動操作之前調用這個方法開始更新

- (void)beginUpdates;

2.對表格控件執(zhí)行多個連續(xù)的插入,刪除和移動操作之后調用這個方法結束

- (void)endUpdates;

3.在一個或多個indexPath處插入cell

- (void)insertRowsAtIndexPaths:(NSArray*)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

4.刪除一個或多個indexPath處的cell

- (void)deleteRowsAtIndexPaths:(NSArray*)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

5.將制定indexPath處的cell移動到另個一indexPath處

- (void)moveRowAtIndexPath:(NSIndexPath*)indexPath toIndexPath:(NSIndexPath*)newIndexPath

6.指定的indexSet所包含的一個或多個分區(qū)號對應的位置插入分區(qū)

- (void)insertSections:(NSIndexSet*)sections withRowAnimation:(UITableViewRowAnimation)animation;

7.刪除指定indexSet所包含的一個或多個分區(qū)號所對應的分區(qū)

- (void)deleteSections:(NSIndexSet*)sections withRowAnimation:(UITableViewRowAnimation)animation;

8.將指定分區(qū)移動到另一個位置

- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection

@protocolUITableViewDataSource協(xié)議中

9.該方法返回值決定指定indexPath對應的cell是否可以編輯

- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath;

10.該方法返回值決定指定indexPath對應的cell是否可以移動

- (BOOL)tableView:(UITableView*)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath;

11.當用戶對指定表格行編輯(包括插入和刪除)時觸發(fā)該方法

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath;

12.該方法觸發(fā)移動 ?通常對數(shù)據(jù)進行處理(重要)

- (void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath;

@protocolUITableViewDelegate協(xié)議中

13.開始/完成 編輯時調用的兩個方法

- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath;

- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath*)indexPath;

14.該方法返回值決定了 indexPath處的cell 的編輯狀態(tài) ?返回值為枚舉類型 分別為 None Delete Insert

- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath;

15.該方法決定了 cell處于被編輯狀態(tài)時是否應該縮進 ?若未重寫 所有cell處于編輯狀態(tài)時都會縮進

- (BOOL)tableView:(UITableView*)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath*)indexPath;

UITableViewCell:UIView

這里涉及到自定義UITableViewCell以下為具體步驟以及需要注意到地方

1.首先創(chuàng)建一個類繼承UITableViewCell

2.把自定義cell上到自定義視圖全部設置為屬性(注意:屬性名一定不要和系統(tǒng)屬性命重復e.g.? imageView,textLable,detailTextLable)

3.在cell的初始化方法中對自定義視圖對屬性初始化踪栋,在初始化對時候可以不指定frame(注意陵吸,這里加載到視圖上時加載到contentView上同時注意內存管理)

4.在layoutSubviews方法中完成最后操作通常給出frame(注意,這個方法為系統(tǒng)自帶方法箩言,當一個cell顯示到屏幕上之前,最后調用到一個方法,所有cell到操作包括賦值广凸,調整高度等都已經(jīng)完成一定不要忘記[super layoutSubviews];)

附加:當一個cell被選中的方法

- (void)setSelected:(BOOL)selected animated:(BOOL)animated

一些小操作:

//將單元格的邊框設置為圓角

cell.layer.cornerRadius=12;

cell.layer.masksToBounds=YES;


UITableView-------表視圖--繼承UIScrollView并遵守NSCoding協(xié)議

屬性

frame-------------設置控件的位置和大小

backgroundColor--------設置控件的顏色

style--------獲取表視圖的樣式

dataSource---------設置UITableViewDataSource的代理

delegate---------設置UITableViewDelegate代理

sectionHeaderHeight------設置組表視圖的頭標簽高度

sectionFooterHeight--------設置級表視圖的尾標簽高度

backgroundView----------設置背景視圖,只能寫入

editing----------是否允許編輯蛛枚,默認是NO

allowsSelection----------在非編輯下,行是否可以選中脸哀,默認為YES

allowsSelectionDuringEditing----------控制某一行時蹦浦,是否可以編輯,默認為NO

allowsMultipleSelection--------是否可以選擇多行撞蜂,默認為NO

allowsMutableSelectionDuringEditing----------在選擇多行的情況下盲镶,是否可以編輯,默認為NO

sectionIndexMinimumDisplayRowCount-------------顯示某個組索引列表在右邊當行數(shù)達到這個值蝌诡,默認是NSInteger的最大值

sectionIndexColor------------選擇某個部分的某行改變這一行上文本的顏色

sectionIndexTrackingBackgroundColor--------設置選中某個部分的背景顏色

separatorStyle----------設置單元格分隔線的樣式

separatorColor---------設置選中單元格分隔線的顏色

tableHeaderView---------設置組表的頭標簽視圖

tableFooterView----------設置組表的尾標簽視圖

UITableView類目屬性

section--------獲取當前在哪個組內

row------------獲取當前單元格是第幾行

方法:

初始化方法:

initWithFrame:-----------設置表的大小和位置

initWithFrame:style---------設置表的大小溉贿,位置和樣式(組,單一)

setEditing:----------表格進入編輯狀態(tài)浦旱,無動畫

setEditing: animated:---------表格進入編輯狀態(tài)宇色,有動畫

reloadData---------------刷新整個表視圖

reloadSectionIndexTitles--------刷新索引欄

numberOfSections-----------獲取當前所有的組

numberOfRowsInSection:---------獲取某個組有多少行

rectForSection:----------獲取某個組的位置和大小

rectForHeaderInSection:---------獲取某個組的頭標簽的位置和大小

rectForFooterInSection:-----------獲取某個組的尾標簽的位置和大小

rectForRowAtIndex:-----------獲取某一行的位置和大小

indexPathForRowAtPoint-------------點擊某一個點,判斷是在哪一行上的信息颁湖。

indexPathForCell:------------獲取單元格的信息

indexPathsForRowsInRect:---------在某個區(qū)域里會返回多個單元格信息

cellForRowAtIndexPath:-------------通過單元格路徑得到單元格

visibleCells-----------返回所有可見的單元格

indexPathsForVisibleRows--------返回所有可見行的路徑

headerViewForSection:--------設置頭標簽的視圖

footerViewForSection宣蠕;----------設置尾標簽的視圖

beginUpdates--------只添加或刪除才會更新行數(shù)

endUpdates---------添加或刪除后會調用添加或刪除方法時才會更新

insertSections:withRowAnimation:-----------插入一個或多個組,并使用動畫

insertRowsIndexPaths:withRowAnimation:-------插入一個或多個單元格甥捺,并使用動畫

deleteSections:withRowAnimation:--------刪除一個或多個組抢蚀,并使用動畫

deleteRowIndexPaths:withRowAnimation:--------刪除一個或多個單元格,并使用動畫

reloadSections:withRowAnimation:---------更新一個或多個組镰禾,并使用動畫

reloadRowIndexPaths:withRowAnimation:-------------更新一個或多個單元格皿曲,并使用動畫

moveSection:toSection:-------------移動某個組到目標組位置

moveRowAtIndexPath:toIndexPath:-----------移動個某個單元格到目標單元格位置

indexPathsForSelectedRow----------返回選擇的一個單元格的路徑

indexPathsForSelectedRows---------返回選擇的所有的單元格的路徑

selectRowAtIndexPath:animation:scrollPosition---------設置選中某個區(qū)域內的單元格

deselectRowAtIndexPath:animation:----------取消選中的單元格

重用機制

dequeueReusableCellWithIdentifier:---------獲取重用隊列里的單元格

UITableViewDataSource代理方法:

方法:

numberOfSectionsInTableView:------------設置表格的組數(shù)

tableView:numberOfRowInSection:----------設置每個組有多少行

tableView:cellForRowAtIndexPath:---------設置單元格顯示的內容

tableView:titleForHeaderInSection:---------設置組表的頭標簽視圖

tableView:titleForFooterInSection:-----------設置組表的尾標簽視圖

tableView:canEditRowAtIndexPath:---------設置單元格是否可以編輯

tableView:canMoveRowAtIndexPath:--------設置單元格是否可以移動

tableView:sectionIndexTitleForTableView:atIndex:-------設置指定組的表的頭標簽文本

tableView:commitEditingStyle:forRowAtIndexPath:----------編輯單元格(添加唱逢,刪除)

tableView:moveRowAtIndexPath:toIndexPath-------單元格移動

UITableViewDelegate代理方法:

tableView:willDisplayCell: forRowAtIndexPath:-----------設置當前的單元格

tableView: heightForRowAtIndexPath:-----------設置每行的高度

tableView:tableView heightForHeaderInSection:-----------設置組表的頭標簽高度

tableView:tableView heightForFooterInSection:-------------設置組表的尾標簽高度

tableView: viewForHeaderInSection:----------自定義組表的頭標簽視圖

tableView: viewForFooterInSection: ----------自定義組表的尾標簽視圖

tableView: accessoryButtonTappedForRowWithIndexPath:-----------設置某個單元格上的右指向按鈕的響應方法

tableView: willSelectRowAtIndexPath:-----------獲取將要選擇的單元格的路徑

tableView: didSelectRowAtIndexPath:-----------獲取選中的單元格的響應事件

tableView: tableView willDeselectRowAtIndexPath:------------獲取將要未選中的單元格的路徑

tableView: didDeselectRowAtIndexPath:-----------獲取未選中的單元格響應事件

執(zhí)行順序如下:

第一輪:

1、numberOfSectionsInTableView ? ?:假如section=2屋休,此函數(shù)只執(zhí)行一次坞古,假如section=0,下面函數(shù)不執(zhí)行博投,默認為1

2绸贡、heightForHeaderInSection ?,執(zhí)行兩次毅哗,此函數(shù)執(zhí)行次數(shù)為section數(shù)目

3听怕、heightForFooterInSection ?,函數(shù)屬性同上虑绵,執(zhí)行兩次

4尿瞭、numberOfRowsInSection ? ?,此方法執(zhí)行一次

5翅睛、heightForHeaderInSection ? ? 声搁,此方法執(zhí)行了兩次,我其實有點困惑為什么這里還要調用這個方法

6捕发、heightForFooterInSection ? 疏旨,此方法執(zhí)行兩次,

7扎酷、numberOfRowsInSection檐涝,執(zhí)行一次

8、heightForRowAtIndexPath ?法挨,行高谁榜,先執(zhí)行section=0,對應的row次數(shù)

第二輪:

1凡纳、numberOfSectionsInTableView 窃植,一次

2、heightForHeaderInSection ?荐糜,section次數(shù)

3巷怜、heightForFooterInSection ? ?,section次數(shù)

4暴氏、numberOfRowsInSection ? ?丛版,一次

5、heightForHeaderInSection ?偏序,執(zhí)行section次數(shù)

6页畦、heightForFooterInSection,執(zhí)行section次數(shù)

7研儒、numberOfRowsInSection豫缨,執(zhí)行一次

8独令、heightForRowAtIndexPath,行高好芭,先執(zhí)行一次

9燃箭、cellForRowAtIndexPath

10、willDisplayCell

然后8舍败、9招狸、10依次執(zhí)行直到所有的cell被描畫完畢

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市邻薯,隨后出現(xiàn)的幾起案子裙戏,更是在濱河造成了極大的恐慌,老刑警劉巖厕诡,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件累榜,死亡現(xiàn)場離奇詭異,居然都是意外死亡灵嫌,警方通過查閱死者的電腦和手機壹罚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來寿羞,“玉大人猖凛,你說我怎么就攤上這事⌒髂拢” “怎么了形病?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長霞幅。 經(jīng)常有香客問我,道長量瓜,這世上最難降的妖魔是什么司恳? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮绍傲,結果婚禮上扔傅,老公的妹妹穿的比我還像新娘。我一直安慰自己烫饼,他們只是感情好猎塞,可當我...
    茶點故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著杠纵,像睡著了一般荠耽。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上比藻,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天铝量,我揣著相機與錄音倘屹,去河邊找鬼。 笑死慢叨,一個胖子當著我的面吹牛纽匙,可吹牛的內容都是我干的。 我是一名探鬼主播拍谐,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼烛缔,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了轩拨?” 一聲冷哼從身側響起践瓷,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎气嫁,沒想到半個月后当窗,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡寸宵,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年崖面,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片梯影。...
    茶點故事閱讀 40,030評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡巫员,死狀恐怖,靈堂內的尸體忽然破棺而出甲棍,到底是詐尸還是另有隱情简识,我是刑警寧澤,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布感猛,位于F島的核電站七扰,受9級特大地震影響,放射性物質發(fā)生泄漏陪白。R本人自食惡果不足惜颈走,卻給世界環(huán)境...
    茶點故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望咱士。 院中可真熱鬧立由,春花似錦、人聲如沸序厉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽弛房。三九已至道盏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背捞奕。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工牺堰, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人颅围。 一個月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓伟葫,卻偏偏與公主長得像,于是被迫代替她去往敵國和親院促。 傳聞我的和親對象是個殘疾皇子筏养,可洞房花燭夜當晚...
    茶點故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內容