UITableView方法總結(jié)(很全面)

UITableView : UIScrollView <NSCoding>

1.創(chuàng)建一個UITableView對象
UITableView *tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];

2.separatorColor
分割線顏色
e.g. tableView.separatorColor = [UIColor redColor];

3.rowHeight
調(diào)整每個cell 點高度(默認 44)
e.g. tableView.rowHeight = 60;

4.reloadData
刷新數(shù)據(jù)
e.g. [tableView reloadData];

5.<UITableViewDelegate,UITableViewDataSource>
兩個必須實現(xiàn)的方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
控制一個section中cell 的多少
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)
indexPath
控制cell中的內(nèi)容

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顯示的內(nèi)容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

10.section上Footer顯示的內(nèi)容
- (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相關(guān):
1.返回表格中指定indexPath對應(yīng)的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對應(yīng)indexPath所組成的數(shù)組
- (NSArray *)indexPathsForVisibleRows;
7.控制該表格滾動到指定indexPath對應(yīng)的cell的頂端 中間 或者下方
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

8.控制該表格滾動到選中cell的頂端 中間 或者下方
-(void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

**處理單元格的選中**
1.@property(nonatomic) BOOL allowsSelection
控制該表格是否允許被選中
2.@property(nonatomic) BOOL allowsMultipleSelection
控制該表格是否允許多選
3.@property(nonatomic) BOOL allowsSelectionDuringEditing;
控制表格處于編輯狀態(tài)時是否允許被選中
4.@property(nonatomic) BOOL allowsMultipleSelectionDuringEditing
控制表格處于編輯狀態(tài)時是否允許被多選

5.獲取選中cell對應(yīng)的indexPath
- (NSIndexPath *)indexPathForSelectedRow;
6.獲取所有被選中的cell對應(yīng)的indexPath組成的數(shù)組
- (NSArray *)indexPathsForSelectedRows
7.控制該表格選中指定indexPath對應(yīng)的表格行,最后一個參數(shù)控制是否滾動到被選中行的頂端 中間 和底部
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
8.控制取消選中該表格中指定indexPath對應(yīng)的表格行
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

<UITableViewDelegate>
9.當(dāng)用戶將要選中表格中的某行時觸發(fā)方法
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
10.當(dāng)用戶完成選中表格中的某行時觸發(fā)方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
11.當(dāng)用戶將要取消選中表格中某行時觸發(fā)
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
12.當(dāng)用戶完成取消選中表格中某行時觸發(fā)
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath

關(guān)于對表格的編輯
1.對表格控件執(zhí)行多個連續(xù)的插入,刪除和移動操作之前調(diào)用這個方法開始更新
- (void)beginUpdates;
2.對表格控件執(zhí)行多個連續(xù)的插入,刪除和移動操作之后調(diào)用這個方法結(jié)束
- (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ū)號對應(yīng)的位置插入分區(qū)
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
7.刪除指定indexSet所包含的一個或多個分區(qū)號所對應(yīng)的分區(qū)
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
8.將指定分區(qū)移動到另一個位置
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection

**@protocol UITableViewDataSource<NSObject>**

9.該方法返回值決定指定indexPath對應(yīng)的cell是否可以編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath;
10.該方法返回值決定指定indexPath對應(yīng)的cell是否可以移動
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath;
11.當(dāng)用戶對指定表格行編輯(包括插入和刪除)時觸發(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;

**@protocol UITableViewDelegate<NSObject,UIScrollViewDelegate>**

13.開始/完成 編輯時調(diào)用的兩個方法
- (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)時是否應(yīng)該縮進  若未重寫 所有cell處于編輯狀態(tài)時都會縮進
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;

**UITableViewCell** : UIView <NSCoding, UIGestureRecognizerDelegate>
這里涉及到自定義UITableViewCell 以下為具體步驟以及需要注意到地方
1.首先創(chuàng)建一個類繼承UITableViewCell
2.把自定義cell上到自定義視圖全部設(shè)置為屬性(注意:屬性名一定不要和系統(tǒng)屬性命重復(fù) e.g.  imageView,textLable,detailTextLable)
3.在cell的初始化方法中 對自定義視圖對屬性初始化休里,在初始化對時候可以不指定frame(注意滓窍,這里加載到視圖上時 加載到contentView 上  同時注意內(nèi)存管理)
4.在layoutSubviews方法中完成最后操作 通常給出frame(注意菱农,這個方法為系統(tǒng)自帶方法醇坝,當(dāng)一個cell顯示到屏幕上之前,最后調(diào)用到一個方法窍仰, 所有cell到操作 包括賦值将硝,調(diào)整高度等 都已經(jīng)完成  一定不要忘記[super layoutSubviews];)

附加:當(dāng)一個cell被選中的方法
 - (void)setSelected:(BOOL)selected animated:(BOOL)animated

一些小操作:
//將單元格的邊框設(shè)置為圓角
cell.layer.cornerRadius = 12;

cell.layer.masksToBounds = YES;

** 再次聲明本文轉(zhuǎn)自:http://blog.csdn.net/qq11231325**


update:以下內(nèi)容為自己添加

  1. 選中tableView中的某一行

     - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;//定位到tableView的某一行
     //示例
     NSIndexPath *ip=[NSIndexPath indexPathForRow:0 inSection:0];
     [self.listView.tabView selectRowAtIndexPath:ip animated:NO scrollPosition:UITableViewScrollPositionNone];//選中第一分區(qū)第一行
    
  2. 待更新

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市哭尝,隨后出現(xiàn)的幾起案子哥攘,更是在濱河造成了極大的恐慌,老刑警劉巖材鹦,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件逝淹,死亡現(xiàn)場離奇詭異,居然都是意外死亡桶唐,警方通過查閱死者的電腦和手機栅葡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來尤泽,“玉大人欣簇,你說我怎么就攤上這事规脸。” “怎么了熊咽?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵莫鸭,是天一觀的道長。 經(jīng)常有香客問我横殴,道長被因,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任滥玷,我火速辦了婚禮氏身,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘惑畴。我一直安慰自己蛋欣,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布如贷。 她就那樣靜靜地躺著陷虎,像睡著了一般。 火紅的嫁衣襯著肌膚如雪杠袱。 梳的紋絲不亂的頭發(fā)上尚猿,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天,我揣著相機與錄音楣富,去河邊找鬼凿掂。 笑死,一個胖子當(dāng)著我的面吹牛纹蝴,可吹牛的內(nèi)容都是我干的庄萎。 我是一名探鬼主播,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼塘安,長吁一口氣:“原來是場噩夢啊……” “哼糠涛!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起兼犯,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤忍捡,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后切黔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體砸脊,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年纬霞,在試婚紗的時候發(fā)現(xiàn)自己被綠了脓规。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡险领,死狀恐怖侨舆,靈堂內(nèi)的尸體忽然破棺而出秒紧,到底是詐尸還是另有隱情,我是刑警寧澤挨下,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布熔恢,位于F島的核電站,受9級特大地震影響臭笆,放射性物質(zhì)發(fā)生泄漏叙淌。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一愁铺、第九天 我趴在偏房一處隱蔽的房頂上張望鹰霍。 院中可真熱鬧,春花似錦茵乱、人聲如沸茂洒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽督勺。三九已至,卻和暖如春斤贰,著一層夾襖步出監(jiān)牢的瞬間智哀,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工荧恍, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留瓷叫,地道東北人。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓送巡,卻偏偏與公主長得像赞辩,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子授艰,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,611評論 2 353

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