前言: 學(xué)iOS也有段時(shí)間了,由于當(dāng)初學(xué)的時(shí)候沒有基礎(chǔ)碱蒙,現(xiàn)在反過來看自己一些基礎(chǔ)控件的用法都還沒搞清楚荠瘪,所以想總結(jié)一些重要控件的用法 -- 本文大部分轉(zhuǎn)載自 -> 琿少博客
文章索引
01 tableView 屬性
02 tableView 方法
03 tableView 動(dòng)畫塊操作
04 tableView 編輯操作
05 tableViewCell 選中相關(guān)操作
06 tableView附件的相關(guān)方法
07 tableView datasource代理方法
08 tableView delegate代理方法
pragma mark -- 01 tableView 屬性
1.常用屬性
設(shè)置表示圖的行高(默認(rèn)為44)
@property (nonatomic)CGFloat rowHeight;
設(shè)置分組樣式時(shí)的頭視圖高度和尾視圖高度(當(dāng)代理方法沒有實(shí)現(xiàn)時(shí)才有效)
@property (nonatomic) CGFloat sectionHeaderHeight;
@property (nonatomic) CGFloat sectionFooterHeight;
設(shè)置一個(gè)行高的估計(jì)值(默認(rèn)為0夯巷,表示沒有估計(jì),7.0之后可用)
@property (nonatomic) CGFloat estimatedRowHeight;
注意:這個(gè)屬性官方的解釋是如果你的tableView的行高是可變的,那么設(shè)計(jì)一個(gè)估計(jì)高度可以加快代碼的運(yùn)行效率哀墓。
下面這兩個(gè)屬性和上面相似趁餐,分別設(shè)置分區(qū)頭視圖和尾視圖的估計(jì)高度(7.0之后可用)
@property (nonatomic) CGFloat estimatedSectionHeaderHeight;
@property (nonatomic) CGFloat estimatedSectionFooterHeight;
設(shè)置分割線的位置
@property (nonatomic) UIEdgeInsets separatorInset;
如果細(xì)心,你可能會(huì)發(fā)現(xiàn)系統(tǒng)默認(rèn)的tableView的分割線左端并沒有頂?shù)竭呇乩捍隆Mㄟ^這個(gè)屬性后雷,可以手動(dòng)設(shè)置分割線的位置偏移,比如你向讓tableView的分割線只顯示右半邊吠各,可以如下設(shè)置:
tableView.separatorInset=UIEdgeInsetsMake(0, tab.frame.size.width/2, 0,0);
設(shè)置tableView背景view視圖
@property(nonatomic, readwrite, retain) UIView *backgroundView;
pragma mark -- 02 tableView 方法
刷新tableView
- (void)reloadData;
刷新索引欄
- (void)reloadSectionIndexTitles;
這個(gè)方法常用語新加或者刪除了索引類別而無需刷新整個(gè)表視圖的情況下臀突。
獲取分組數(shù)
- (NSInteger)numberOfSections;
根據(jù)分組獲取行數(shù)
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
獲取分組的大小(包括頭視圖,所有行和尾視圖)
- (CGRect)rectForSection:(NSInteger)section;
根據(jù)分組分別獲取頭視圖贾漏,尾視圖和行的高度
- (CGRect)rectForHeaderInSection:(NSInteger)section;
- (CGRect)rectForFooterInSection:(NSInteger)section;
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
獲取某個(gè)點(diǎn)在tableView中的位置信息
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
獲取某個(gè)cell在tableView中的位置信息
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;
根據(jù)一個(gè)矩形范圍返回一個(gè)信息數(shù)組候学,數(shù)組中是每一行row的位置信息
- (NSArray *)indexPathsForRowsInRect:(CGRect)rect;
通過位置路徑獲取cell
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
獲取所有可見的cell
- (NSArray *)visibleCells;
獲取所有可見行的位置信息
- (NSArray *)indexPathsForVisibleRows;
根據(jù)分組獲取頭視圖
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section;
根據(jù)分組獲取尾視圖
- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section;
使表格示圖定位到某一位置(行)
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
注意:indexPah參數(shù)是定位的位置,決定于分區(qū)和行號(hào)纵散。animated參數(shù)決定是否有動(dòng)畫梳码。scrollPosition參數(shù)決定定位的相對(duì)位置,它使一個(gè)枚舉伍掀,如下:
typedef NS_ENUM(NSInteger, UITableViewScrollPosition)
{
UITableViewScrollPositionNone,//同UITableViewScrollPositionTop
UITableViewScrollPositionTop,//定位完成后掰茶,將定位的行顯示在tableView的頂部
UITableViewScrollPositionMiddle,//定位完成后,將定位的行顯示在tableView的中間
UITableViewScrollPositionBottom//定位完成后蜜笤,將定位的行顯示在tableView最下面
};
使表格示圖定位到選中行
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
這個(gè)函數(shù)與上面的非常相似濒蒋,只是它是將表示圖定位到選中的行。
pragma mark -- 03 tableView 動(dòng)畫塊操作
在介紹動(dòng)畫塊之前把兔,我們先看幾個(gè)函數(shù):
插入分區(qū)
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
animation參數(shù)是一個(gè)枚舉沪伙,枚舉的動(dòng)畫類型如下
typedef NS_ENUM(NSInteger, UITableViewRowAnimation)
{
UITableViewRowAnimationFade,//淡入淡出
UITableViewRowAnimationRight,//從右滑入
UITableViewRowAnimationLeft,//從左滑入
UITableViewRowAnimationTop,//從上滑入
UITableViewRowAnimationBottom,//從下滑入
UITableViewRowAnimationNone, //沒有動(dòng)畫
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100 // 自動(dòng)選擇合適的動(dòng)畫
};
刪除分區(qū)
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
重載一個(gè)分區(qū)
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation ;
移動(dòng)一個(gè)分區(qū)
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;
插入一些行
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
刪除一些行
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
重載一些行
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
移動(dòng)某行
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;
了解了上面幾個(gè)函數(shù),我們來看什么是操作刷新塊:
當(dāng)我們調(diào)用的上面的函數(shù)時(shí)垛贤,tableView會(huì)立刻調(diào)用代理方法進(jìn)行刷新焰坪,如果其中我們所做的操作是刪除某行,而然數(shù)據(jù)源數(shù)組我們可能并沒有刷新聘惦,程序就會(huì)崩潰掉某饰,原因是代理返回的信息和我們刪除后不符儒恋。
IOS為我們提供了下面兩個(gè)函數(shù)解決這個(gè)問題:
開始?jí)K標(biāo)志
- (void)beginUpdates;
結(jié)束快標(biāo)志
- (void)endUpdates;
我們可以將我們要做的操作全部寫在這個(gè)塊中,那么黔漂,只有當(dāng)程序執(zhí)行到結(jié)束快標(biāo)志后诫尽,才會(huì)調(diào)用代理刷新方法。代碼示例如下:
[tab beginUpdates];
[tab deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITabl eViewRowAnimationLeft];
[dataArray removeObjectAtIndex:1];
[tab endUpdates];
注意:不要在這個(gè)塊中調(diào)用reloadData這個(gè)方法炬守,它會(huì)使動(dòng)畫失效牧嫉。
pragma mark -- 04 tableView 編輯操作
設(shè)置是否是編輯狀態(tài)(編輯狀態(tài)下的cell左邊會(huì)出現(xiàn)一個(gè)減號(hào),點(diǎn)擊右邊會(huì)劃出刪除按鈕)
@property (nonatomic, getter=isEditing) BOOL editing;
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
設(shè)置cell是否可以被選中(默認(rèn)為YES)
@property (nonatomic) BOOL allowsSelection;
設(shè)置cell編輯模式下是否可以被選中
@property (nonatomic) BOOL allowsSelectionDuringEditing;
設(shè)置是否支持多選
@property (nonatomic) BOOL allowsMultipleSelection;
設(shè)置編輯模式下是否支持多選
@property (nonatomic) BOOL allowsMultipleSelectionDuringEditing;
pragma mark -- 05 tableViewCell 選中相關(guān)操作
獲取選中cell的位置信息
- (NSIndexPath *)indexPathForSelectedRow;
獲取多選cell的位置信息
- (NSArray *)indexPathsForSelectedRows;
代碼手動(dòng)選中與取消選中某行
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
注意:這兩個(gè)方法將不會(huì)回調(diào)代理中的方法减途。
pragma mark -- 06 tableView附件的相關(guān)方法
設(shè)置索引欄最小顯示行數(shù)
@property (nonatomic) NSInteger sectionIndexMinimumDisplayRowCount;
設(shè)置索引欄字體顏色
@property (nonatomic, retain) UIColor *sectionIndexColor;
設(shè)置索引欄背景顏色
@property (nonatomic, retain) UIColor *sectionIndexBackgroundColor;
設(shè)置索引欄被選中時(shí)的顏色
@property (nonatomic, retain) UIColor *sectionIndexTrackingBackgroundColor;
設(shè)置分割線的風(fēng)格
@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle;
這個(gè)風(fēng)格是一個(gè)枚舉酣藻,如下:
typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle)
{
UITableViewCellSeparatorStyleNone,//無線
UITableViewCellSeparatorStyleSingleLine,//有線
UITableViewCellSeparatorStyleSingleLineEtched
};
設(shè)置分割線顏色
@property (nonatomic, retain) UIColor *separatorColor;
設(shè)置分割線毛玻璃效果(IOS8之后可用)
@property (nonatomic, copy) UIVisualEffect *separatorEffect;
注意:這個(gè)屬性是IOS8之后新的。
設(shè)置tableView頭視圖
@property (nonatomic, retain) UIView *tableHeaderView;
設(shè)置tableView尾視圖
@property (nonatomic, retain) UIView *tableFooterView;
從復(fù)用池中取cell
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
獲取一個(gè)已注冊(cè)的cell
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
從復(fù)用池獲取頭視圖或尾視圖
- (id)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier;
通過xib文件注冊(cè)cell
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier;
通過OC類注冊(cè)cell
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier
上面兩個(gè)方法是IOS6之后的方法鳍置。
通過xib文件和OC類獲取注冊(cè)頭視圖和尾視圖
- (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier;
- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)
pragma mark -- 07 tableView datasource代理方法
返回每個(gè)分區(qū)的行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
返回每一行的cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
可選實(shí)現(xiàn)的方法
返回分區(qū)數(shù)(默認(rèn)為1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
返回每個(gè)分區(qū)頭部的標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
返回每個(gè)分區(qū)的尾部標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
設(shè)置某行是否可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
設(shè)置某行是否可以被移動(dòng)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
設(shè)置索引欄標(biāo)題數(shù)組(實(shí)現(xiàn)這個(gè)方法辽剧,會(huì)在tableView右邊顯示每個(gè)分區(qū)的索引)
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
設(shè)置索引欄標(biāo)題對(duì)應(yīng)的分區(qū)
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
tableView接受編輯時(shí)調(diào)用的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
這個(gè)方法中的editingStyle參數(shù)是一個(gè)枚舉,代表了cell被編輯的模式税产,如下:
typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle)
{
UITableViewCellEditingStyleNone,//沒有編輯操作
UITableViewCellEditingStyleDelete,//刪除操作
UITableViewCellEditingStyleInsert//插入操作
};
tableView的cell被移動(dòng)時(shí)調(diào)用的方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
pragma mark -- 08 tableView delegate代理方法
cell將要顯示時(shí)調(diào)用的方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
頭視圖將要顯示時(shí)調(diào)用的方法
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section;
尾視圖將要顯示時(shí)調(diào)用的方法
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section;
和上面的方法對(duì)應(yīng)怕轿,這三個(gè)方法分別是cell,頭視圖辟拷,尾視圖已經(jīng)顯示時(shí)調(diào)用的方法
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath;
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section;
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section;
設(shè)置行高撞羽,頭視圖高度和尾視圖高度的方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
設(shè)置行高,頭視圖高度和尾視圖高度的估計(jì)值(對(duì)于高度可變的情況下衫冻,提高效率)
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath*)indexPath;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section;
設(shè)置自定義頭視圖和尾視圖
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
設(shè)置cell是否可以高亮
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath;
cell高亮和取消高亮?xí)r分別調(diào)用的函數(shù)
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath;
當(dāng)即將選中某行和取消選中某行時(shí)調(diào)用的函數(shù)诀紊,返回一直位置,執(zhí)行選中或者取消選中
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath*)indexPath;
已經(jīng)選中和已經(jīng)取消選中后調(diào)用的函數(shù)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
設(shè)置tableView被編輯時(shí)的狀態(tài)風(fēng)格羽杰,如果不設(shè)置渡紫,默認(rèn)都是刪除風(fēng)格
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
自定義刪除按鈕的標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath;
下面這個(gè)方法是IOS8中的新方法,用于自定義創(chuàng)建tableView被編輯時(shí)右邊的按鈕考赛,按鈕類型為UITableViewRowAction惕澎。
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath;
設(shè)置編輯時(shí)背景是否縮進(jìn)
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
將要編輯和結(jié)束編輯時(shí)調(diào)用的方法
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath;
移動(dòng)特定的某行
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;