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.當用戶將要選中表格中的某行時觸發(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

關(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.當用戶對指定表格行編輯(包括插入和刪除)時觸發(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)自帶方法,當一個cell顯示到屏幕上之前,最后調(diào)用到一個方法, 所有cell到操作 包括賦值,調(diào)整高度等 都已經(jīng)完成  一定不要忘記[super layoutSubviews];)

附加:當一個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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容