iOS中UITableView使用總結(jié)

一、初始化方法- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style;? 這個(gè)方法初始化表視圖的frame大小并且設(shè)置一個(gè)風(fēng)格,UITableViewStyle是一個(gè)枚舉,如下:

typedef NS_ENUM(NSInteger, UITableViewStyle) {? ??

UITableViewStylePlain,? ? ? ? ? ? ? ? ? // 標(biāo)準(zhǔn)的表視圖風(fēng)格? ??

UITableViewStyleGrouped? ? ? ? ? ? ? ? // 分組的表視圖風(fēng)格};

二、常用屬性獲取表視圖的風(fēng)格(只讀屬性)@property (nonatomic, readonly) UITableViewStyle? ? ? ? ? style;設(shè)置表示圖代理和數(shù)據(jù)源代理(代理方法后面討論)@property (nonatomic, assign)? iddataSource;@property (nonatomic, assign)? iddelegate;

設(shè)置表示圖的行高(默認(rèn)為44)

@property (nonatomic)CGFloat rowHeight;

設(shè)置分區(qū)的頭視圖高度和尾視圖高度(當(dāng)代理方法沒(méi)有實(shí)現(xiàn)時(shí)才有效)

@property (nonatomic)? ? ? ? ? CGFloat? ? ? ? ? ? ? ? ? ? sectionHeaderHeight;

@property (nonatomic)? ? ? ? ? CGFloat? ? ? ? ? ? ? ? ? ? sectionFooterHeight;

設(shè)置一個(gè)行高的估計(jì)值(默認(rèn)為0,表示沒(méi)有估計(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的分割線左端并沒(méi)有頂?shù)竭呇亍Mㄟ^(guò)這個(gè)屬性,可以手動(dòng)設(shè)置分割線的位置偏移,比如你向讓tableView的分割線只顯示右半邊,可以如下設(shè)置:

UITableView * tab = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

tab.separatorInset=UIEdgeInsetsMake(0, tab.frame.size.width/2, 0,0);

設(shè)置tableView背景view視圖

@property(nonatomic, readwrite, retain) UIView *backgroundView;

三、常用方法詳解

重載tableView

- (void)reloadData;

重載索引欄

- (void)reloadSectionIndexTitles;

這個(gè)方法常用語(yǔ)新加或者刪除了索引類別而無(wú)需刷新整個(gè)表視圖的情況下。

獲取分區(qū)數(shù)

- (NSInteger)numberOfSections;

根據(jù)分區(qū)獲取行數(shù)

- (NSInteger)numberOfRowsInSection:(NSInteger)section;

獲取分區(qū)的大小(包括頭視圖,所有行和尾視圖)

- (CGRect)rectForSection:(NSInteger)section;

根據(jù)分區(qū)分別獲取頭視圖,尾視圖和行的高度

- (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;

通過(guò)位置路徑獲取cell

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

獲取所有可見(jiàn)的cell

- (NSArray *)visibleCells;

獲取所有可見(jiàn)行的位置信息

- (NSArray *)indexPathsForVisibleRows;

根據(jù)分區(qū)獲取頭視圖

- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section;

根據(jù)分區(qū)獲取尾視圖

- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section;

使表示圖定位到某一位置(行)

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

注意:indexPah參數(shù)是定位的位置,決定于分區(qū)和行號(hào)。animated參數(shù)決定是否有動(dòng)畫(huà)。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ù)與上面的非常相似,只是它是將表示圖定位到選中的行。

四、tableView操作刷新塊的應(yīng)用

在介紹動(dòng)畫(huà)塊之前,我們先看幾個(gè)函數(shù):

插入分區(qū)

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

animation參數(shù)是一個(gè)枚舉,枚舉的動(dòng)畫(huà)類型如下

typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {

UITableViewRowAnimationFade,//淡入淡出

UITableViewRowAnimationRight,//從右滑入

UITableViewRowAnimationLeft,//從左滑入

UITableViewRowAnimationTop,//從上滑入

UITableViewRowAnimationBottom,//從下滑入

UITableViewRowAnimationNone,? //沒(méi)有動(dòng)畫(huà)

UITableViewRowAnimationMiddle,

UITableViewRowAnimationAutomatic = 100? // 自動(dòng)選擇合適的動(dòng)畫(huà)

};

刪除分區(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ù),我們來(lái)看什么是操作刷新塊:

當(dāng)我們調(diào)用的上面的函數(shù)時(shí),tableView會(huì)立刻調(diào)用代理方法進(jìn)行刷新,如果其中我們所做的操作是刪除某行,而然數(shù)據(jù)源數(shù)組我們可能并沒(méi)有刷新,程序就會(huì)崩潰掉,原因是代理返回的信息和我們刪除后不符。

IOS為我們提供了下面兩個(gè)函數(shù)解決這個(gè)問(wèn)題:

開(kāi)始?jí)K標(biāo)志

- (void)beginUpdates;

結(jié)束快標(biāo)志

- (void)endUpdates;

我們可以將我們要做的操作全部寫(xiě)在這個(gè)塊中,那么,只有當(dāng)程序執(zhí)行到結(jié)束快標(biāo)志后,才會(huì)調(diào)用代理刷新方法。代碼示例如下:

[tab beginUpdates];

[tab deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];

[dataArray removeObjectAtIndex:1];

[tab endUpdates];

注意:不要在這個(gè)塊中調(diào)用reloadData這個(gè)方法,它會(huì)使動(dòng)畫(huà)失效。

五、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;

六、選中cell的相關(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)代理中的方法。

七、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,//無(wú)線

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;

通過(guò)xib文件注冊(cè)cell

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier;

通過(guò)OC類注冊(cè)cell

- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier

上面兩個(gè)方法是IOS6之后的方法。

通過(guò)xib文件和OC類獲取注冊(cè)頭視圖和尾視圖

- (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier;

- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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