UITableView使用詳解

一、初始化方法
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style;  
這個方法初始化表視圖的frame大小并且設置一個風格

UITableViewStyle是一個枚舉,如下:
typedef NS_ENUM(NSInteger, UITableViewStyle) {    
UITableViewStylePlain,       // 標準的表視圖風格    
UITableViewStyleGrouped       // 分組的表視圖風格};
二、常用屬性

1. 獲取表視圖的風格(只讀屬性)

@property(nonatomic, readonly) UITableViewStyle style;

2. 設置表示圖代理和數(shù)據(jù)源代理

@property(nonatomic, assign)  id <UITableViewDataSource> dataSource;
@property(nonatomic, assign)  id <UITableViewDelegate> delegate;

3. 設置表示圖的行高(默認為44)

@property(nonatomic)  CGFloat rowHeight; 

設置分區(qū)的頭視圖高度和尾視圖高度(當代理方法沒有實現(xiàn)時才有效)

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

4. 設置一個行高的估計值(默認為0,表示沒有估計,7.0之后可用)

@property (nonatomic) CGFloat estimatedRowHeight; 
注意:這個屬性官方的解釋是如果你的tableView的行高是可變的,那么設計一個估計高度可以加快代碼的運行效率。

下面這兩個屬性和上面相似,分別設置分區(qū)頭視圖和尾視圖的估計高度(7.0之后可用)
@property (nonatomic) CGFloat estimatedSectionHeaderHeight; 
@property (nonatomic) CGFloat estimatedSectionFooterHeight;

5. 設置分割線的位置

@property (nonatomic) UIEdgeInsets separatorInset;
如果細心,你可能會發(fā)現(xiàn)系統(tǒng)默認的tableView的分割線左端并沒有頂?shù)竭呇亍Mㄟ^這個屬性,可以手動設置分割線的位置偏移,比如你想讓tableView的分割線只顯示右半邊,可以如下設置:
UITableView * tab = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
tab.separatorInset = UIEdgeInsetsMake(0, tab.frame.size.width/2, 0,0);

6. 設置tableView背景view視圖

@property(nonatomic, readwrite, retain) UIView *backgroundView;
三、常用方法詳解

1. 重載tableView

- (void)reloadData;

2. 重載索引欄

- (void)reloadSectionIndexTitles;
這個方法常用于新加或者刪除了索引類別而無需刷新整個表視圖的情況下。

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

- (NSInteger)numberOfSections;

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

- (NSInteger)numberOfRowsInSection:(NSInteger)section;

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

- (CGRect)rectForSection:(NSInteger)section; 

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

- (CGRect)rectForHeaderInSection:(NSInteger)section;
- (CGRect)rectForFooterInSection:(NSInteger)section;
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;

7. 獲取某個點在tableView中的位置信息

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

8. 獲取某個cell在tableView中的位置信息

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

9. 根據(jù)一個矩形范圍返回一個信息數(shù)組,數(shù)組中是每一行row的位置信息

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

10. 通過位置路徑獲取cell

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

11. 獲取所有可見的cell

- (NSArray *)visibleCells;

12. 獲取所有可見行的位置信息

- (NSArray *)indexPathsForVisibleRows;

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

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

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

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

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

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
注意:indexPah參數(shù)是定位的位置,決定于分區(qū)和行號。animated參數(shù)決定是否有動畫。
scrollPosition參數(shù)決定定位的相對位置,它使一個枚舉,如下:
typedef NS_ENUM(NSInteger, UITableViewScrollPosition) {    
UITableViewScrollPositionNone,//同UITableViewScrollPositionTop  
UITableViewScrollPositionTop,//定位完成后,將定位的行顯示在tableView的頂部        
UITableViewScrollPositionMiddle,//定位完成后,將定位的行顯示在tableView的中間       
UITableViewScrollPositionBottom//定位完成后,將定位的行顯示在tableView最下面
};

16. 使表示圖定位到選中行

- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
這個函數(shù)與上面的非常相似,只是它是將表示圖定位到選中的行。
四、tableView操作刷新塊的應用

在介紹動畫塊之前,我們先看幾個函數(shù):
1. 插入分區(qū)

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
animation參數(shù)是一個枚舉,枚舉的動畫類型如下
typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {    
UITableViewRowAnimationFade,//淡入淡出    
UITableViewRowAnimationRight,//從右滑入    
UITableViewRowAnimationLeft,//從左滑入    
UITableViewRowAnimationTop,//從上滑入    
UITableViewRowAnimationBottom,//從下滑入    
UITableViewRowAnimationNone,  //沒有動畫
UITableViewRowAnimationMiddle,              
UITableViewRowAnimationAutomatic = 100  // 自動選擇合適的動畫
};

2. 刪除分區(qū)

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

3. 重載一個分區(qū)

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

4. 移動一個分區(qū)

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

5. 插入一些行

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

6. 刪除一些行

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

7 .重載一些行

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

8. 移動某行

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

了解了上面幾個函數(shù),我們來看什么是操作刷新塊:

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

IOS為我們提供了下面兩個函數(shù)解決這個問題:
1. 開始塊標志

- (void)beginUpdates; 

2. 結(jié)束快標志

- (void)endUpdates; 

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

[tab beginUpdates];   
[tab deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];    
[dataArray removeObjectAtIndex:1];  
[tab endUpdates];

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

五、tableView的編輯操作

1. 設置是否是編輯狀態(tài)(編輯狀態(tài)下的cell左邊會出現(xiàn)一個減號,點擊右邊會劃出刪除按鈕)

@property(nonatomic, getter=isEditing) BOOL editing;                             
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

2. 設置cell是否可以被選中(默認為YES)

@property(nonatomic) BOOL allowsSelection;

3. 設置cell編輯模式下是否可以被選中

@property(nonatomic) BOOL allowsSelectionDuringEditing;  

4. 設置是否支持多選

@property(nonatomic) BOOL allowsMultipleSelection;

5. 設置編輯模式下是否支持多選

@property(nonatomic) BOOL allowsMultipleSelectionDuringEditing;
六、選中cell的相關(guān)操作

1. 獲取選中cell的位置信息

- (NSIndexPath *)indexPathForSelectedRow; 

2. 獲取多選cell的位置信息

- (NSArray *)indexPathsForSelectedRows;

3. 代碼手動選中與取消選中某行

- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
注意:這兩個方法將不會回調(diào)代理中的方法。
七、tableView附件的相關(guān)方法

1. 設置索引欄最小顯示行數(shù)

@property (nonatomic) NSInteger sectionIndexMinimumDisplayRowCount;  

2. 設置索引欄字體顏色

@property (nonatomic, retain) UIColor *sectionIndexColor;

3. 設置索引欄背景顏色

@property (nonatomic, retain) UIColor *sectionIndexBackgroundColor;

4. 設置索引欄被選中時的顏色

@property (nonatomic, retain) UIColor *sectionIndexTrackingBackgroundColor;

5. 設置分割線的風格

@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle;
這個風格是一個枚舉,如下:
typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {    
UITableViewCellSeparatorStyleNone,//無線    
UITableViewCellSeparatorStyleSingleLine,//有線    
UITableViewCellSeparatorStyleSingleLineEtched  
};

6. 設置分割線顏色

@property(nonatomic, retain) UIColor *separatorColor;

7. 設置分割線毛玻璃效果(IOS8之后可用)

@property(nonatomic, copy) UIVisualEffect *separatorEffect;
注意:這個屬性是IOS8之后新的。

8. 設置tableView頭視圖

@property(nonatomic, retain) UIView *tableHeaderView;  

9. 設置tableView尾視圖

@property(nonatomic, retain) UIView *tableFooterView; 

10. 從復用池中取cell

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;

11. 獲取一個已注冊的cell

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath

12. 從復用池獲取頭視圖或尾視圖

- (id)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier;

13. 通過xib文件注冊cell

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

14. 通過OC類注冊cell

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

上面兩個方法是IOS6之后的方法。
15. 通過xib文件和OC類獲取注冊頭視圖和尾視圖

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

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