- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style; 數組
這個方法初始化表視圖的frame大小而且設置一個風格,UITableViewStyle是一個枚舉,以下:ide
?函數
1
2
3
4
|
typedef
NS_ENUM(NSInteger, UITableViewStyle) {
UITableViewStylePlain,
// 標準的表視圖風格
UITableViewStyleGrouped
// 分組的表視圖風格
};
|
獲取表視圖的風格(只讀屬性)字體
@property (nonatomic, readonly) UITableViewStyle style;動畫
設置表示圖代理和數據源代理(代理方法後面討論)atom
@property (nonatomic, assign) id <UITableViewDataSource> dataSource;spa
@property (nonatomic, assign) id <UITableViewDelegate> delegate;.net
設置表示圖的行高(默認爲44)
設計
@property (nonatomic)CGFloat rowHeight; 代理
設置分區的頭視圖高度和尾視圖高度(當代理方法沒有實現時纔有效)
@property (nonatomic) CGFloat sectionHeaderHeight;
@property (nonatomic) CGFloat sectionFooterHeight;
設置一個行高的估計值(默認爲0,表示沒有估計,7.0以後可用)
@property (nonatomic) CGFloat estimatedRowHeight;
注意:這個屬性官方的解釋是若是你的tableView的行高是可變的,那麼設計一個估計高度能夠加快代碼的運行效率。
下面這兩個屬性和上面類似,分別設置分區頭視圖和尾視圖的估計高度(7.0以後可用)
@property (nonatomic) CGFloat estimatedSectionHeaderHeight; @property (nonatomic) CGFloat estimatedSectionFooterHeight;
設置分割線的位置
@property (nonatomic) UIEdgeInsets separatorInset;
若是細心,你可能會發現系統默認的tableView的分割線左端並無頂到邊沿。經過這個屬性,能夠手動設置分割線的位置偏移,好比你向讓tableView的分割線只顯示右半邊,能夠以下設置:
1
2
|
UITableView * tab = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
tab.separatorInset=UIEdgeInsetsMake(0, tab.frame.size.width/2, 0,0);
|
設置tableView背景view視圖
@property(nonatomic, readwrite, retain) UIView *backgroundView;
重載tableView
- (void)reloadData;
重載索引欄
- (void)reloadSectionIndexTitles;
這個方法經常使用語新加或者刪除了索引類別而無需刷新整個表視圖的狀況下。
獲取分區數
- (NSInteger)numberOfSections;
根據分區獲取行數
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
獲取分區的大小(包括頭視圖,全部行和尾視圖)
- (CGRect)rectForSection:(NSInteger)section;
根據分區分別獲取頭視圖,尾視圖和行的高度
- (CGRect)rectForHeaderInSection:(NSInteger)section;
- (CGRect)rectForFooterInSection:(NSInteger)section;
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
獲取某個點在tableView中的位置信息
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
獲取某個cell在tableView中的位置信息
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;
根據一個矩形範圍返回一個信息數組,數組中是每一行row的位置信息
- (NSArray *)indexPathsForRowsInRect:(CGRect)rect;
經過位置路徑獲取cell
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
獲取全部可見的cell
- (NSArray *)visibleCells;
獲取全部可見行的位置信息
- (NSArray *)indexPathsForVisibleRows;
根據分區獲取頭視圖
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section;
根據分區獲取尾視圖
- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section;
使表示圖定位到某一位置(行)
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
注意:indexPah參數是定位的位置,決定於分區和行號。animated參數決定是否有動畫。scrollPosition參數決定定位的相對位置,它使一個枚舉,以下:
1
2
3
4
5
6
|
typedef
NS_ENUM(NSInteger, UITableViewScrollPosition) {
UITableViewScrollPositionNone,
//同UITableViewScrollPositionTop
UITableViewScrollPositionTop,
//定位完成後,將定位的行顯示在tableView的頂部
UITableViewScrollPositionMiddle,
//定位完成後,將定位的行顯示在tableView的中間
UITableViewScrollPositionBottom
//定位完成後,將定位的行顯示在tableView最下面
};
|
使表示圖定位到選中行
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
這個函數與上面的很是類似,只是它是將表示圖定位到選中的行。
在介紹動畫塊以前,咱們先看幾個函數:
插入分區
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
animation參數是一個枚舉,枚舉的動畫類型以下
1
2
3
4
5
6
7
8
9
10
|
typedef
NS_ENUM(NSInteger, UITableViewRowAnimation) {
UITableViewRowAnimationFade,
//淡入淡出
UITableViewRowAnimationRight,
//從右滑入
UITableViewRowAnimationLeft,
//從左滑入
UITableViewRowAnimationTop,
//從上滑入
UITableViewRowAnimationBottom,
//從下滑入
UITableViewRowAnimationNone,
//沒有動畫
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100
// 自動選擇合適的動畫
};
|
刪除分區
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
重載一個分區
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation ;
移動一個分區
- (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;
移動某行
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;
瞭解了上面幾個函數,咱們來看什麼是操做刷新塊:
當咱們調用的上面的函數時,tableView會馬上調用代理方法進行刷新,若是其中咱們所作的操做是刪除某行,而然數據源數組咱們可能並無刷新,程序就會崩潰掉,緣由是代理返回的信息和咱們刪除後不符。
IOS爲咱們提供了下面兩個函數解決這個問題:
開始塊標誌
- (void)beginUpdates;
結束快標誌
- (void)endUpdates;
咱們能夠將咱們要作的操做所有寫在這個塊中,那麼,只有當程序執行到結束快標誌後,纔會調用代理刷新方法。代碼示例以下:
1
2
3
4
|
[tab beginUpdates];
[tab deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
[dataArray removeObjectAtIndex:1];
[tab endUpdates];
|
注意:不要在這個塊中調用reloadData這個方法,它會使動畫失效。
設置是不是編輯狀態(編輯狀態下的cell左邊會出現一個減號,點擊右邊會劃出刪除按鈕)
@property (nonatomic, getter=isEditing) BOOL editing;
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
設置cell是否能夠被選中(默認爲YES)
@property (nonatomic) BOOL allowsSelection;
設置cell編輯模式下是否能夠被選中
@property (nonatomic) BOOL allowsSelectionDuringEditing;
設置是否支持多選
@property (nonatomic) BOOL allowsMultipleSelection;
設置編輯模式下是否支持多選
@property (nonatomic) BOOL allowsMultipleSelectionDuringEditing;
獲取選中cell的位置信息
- (NSIndexPath *)indexPathForSelectedRow;
獲取多選cell的位置信息
- (NSArray *)indexPathsForSelectedRows;
代碼手動選中與取消選中某行
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
注意:這兩個方法將不會回調代理中的方法。
設置索引欄最小顯示行數
@property (nonatomic) NSInteger sectionIndexMinimumDisplayRowCount;
設置索引欄字體顏色
@property (nonatomic, retain) UIColor *sectionIndexColor;
設置索引欄背景顏色
@property (nonatomic, retain) UIColor *sectionIndexBackgroundColor;
設置索引欄被選中時的顏色
@property (nonatomic, retain) UIColor *sectionIndexTrackingBackgroundColor;
設置分割線的風格
@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle;
這個風格是一個枚舉,以下:
1
2
3
4
5
|
typedef
NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
UITableViewCellSeparatorStyleNone,
//無線
UITableViewCellSeparatorStyleSingleLine,
//有線
UITableViewCellSeparatorStyleSingleLineEtched
};
|
設置分割線顏色
@property (nonatomic, retain) UIColor *separatorColor;
設置分割線毛玻璃效果(IOS8以後可用)
@property (nonatomic, copy) UIVisualEffect *separatorEffect;
注意:這個屬性是IOS8以後新的。
設置tableView頭視圖
@property (nonatomic, retain) UIView *tableHeaderView;
設置tableView尾視圖
@property (nonatomic, retain) UIView *tableFooterView;
從複用池中取cell
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
獲取一個已註冊的cell
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
從複用池獲取頭視圖或尾視圖
- (id)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier;
經過xib文件註冊cell
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier;
經過OC類註冊cell
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier
上面兩個方法是IOS6以後的方法。
經過xib文件和OC類獲取註冊頭視圖和尾視圖
- (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier;
- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)
關於tableView的代理方法,由於篇幅緣由,總結在下一篇博客中。