iOS UITableView 常用方法集合

//聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄

實現(xiàn)代理三部曲:

一:遵守代理

二:把tableView.delegate = self

三:實現(xiàn)代理方法

- (void)viewDidLoad {

[superviewDidLoad];

self.view.backgroundColor= [UIColorwhiteColor];

[self.viewaddSubview:self.tableView];

}

#pragma mark - getter/setter

- (UITableView*)tableView {

if(!_tableView) {

_tableView=[[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStyleGrouped];

_tableView.backgroundColor=RGB(239,239,239);

_tableView.showsVerticalScrollIndicator=NO;

_tableView.showsHorizontalScrollIndicator=NO;

_tableView.delegate=self;

_tableView.dataSource=self;

//分割線顏色

_tableView.separatorStyle=UITableViewCellSeparatorStyleNone;

分割線顏色

}

return_tableView;

}

************ UITableView 方法 ************

//多少組

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

return6;

}

//多少行

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

NSArray*arr = [dicobjectForKey:[dic.allKeysobjectAtIndex:section]];

return10;

}

//每行顯示什么內(nèi)容

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

staticNSString*cellId =@"CELLID";

UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:cellId];

if(!cell) {

cell? = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:cellId];

}

returncell;

}

//組頭名字

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

return@"石虎";

}

//點擊選中

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

//跳轉(zhuǎn)到二級界面

SHMessageViewController*mess = [[SHMessageViewControlleralloc]init];

//跳轉(zhuǎn)

[selfpresentViewController:messanimated:YEScompletion:nil];

}

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

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

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

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

return2;

}

//section上Header顯示的內(nèi)容

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

{

return@"section上Header顯示的內(nèi)容";

}

//section上Footer顯示的內(nèi)容

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

{

return@"section上Footer顯示的內(nèi)容";

}

//section頂部的高度

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

{

return20;

}

//cell的高度

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

{

return50;

}

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

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

{

return[NSArrayarray];

}

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

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

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

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

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

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

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

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

************ UITableViewCell方法************

0.當一個cell被選中的方法

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

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

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

2.返回指定cell的indexPath

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

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

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

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

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

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

- ( NSArray *)visibleCells;

6.返回表格中所有可見區(qū)域內(nèi)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;

************單元格的選中--屬性************

0.獲取選中cell對應的indexPath

- ( NSIndexPath *)indexPathForSelectedRow;

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

@property ( nonatomic ) BOOL allowsSelection

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

@property ( nonatomic ) BOOL allowsMultipleSelection

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

@property ( nonatomic ) BOOL allowsSelectionDuringEditing;

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

@property ( nonatomic ) BOOL allowsMultipleSelectionDuringEditing

5.獲取所有被選中的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;

************關于對表格的編輯方法************

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

- ( void )endUpdates;

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

- ( void )beginUpdates;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

12.開始/完成編輯時調(diào)用的兩個方法

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

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

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

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

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

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

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

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,197評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,415評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,104評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,884評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,647評論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,130評論 1 323
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,208評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,366評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,887評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,737評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,939評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,478評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,174評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,586評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,827評論 1 283
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,608評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,914評論 2 372

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