一、初始化方法
1:TableView風格設置。
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style;
這個方法初始化表視圖的frame大小并且設置一個風格,UITableViewStyle是一個枚舉,如下:
typedefNS_ENUM(NSInteger,UITableViewStyle) {
UITableViewStylePlain,// 標準的表視圖風格
UITableViewStyleGrouped// 分組的表視圖風格
};
2:完成tableView的創建? (cell重用)
傳統方法 or ?注冊法(推薦)
1:傳統方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
}
return cell;
}
2:注冊法:(推薦)
1??先在TableView的屬性設置(懶加載)注冊(這里注冊的是Xib的cell,不用Xib的同理)
[tableView registerNib:[UINib nibWithNibName:@"***" bundle:nil] forCellReuseIdentifier:@"Identifier"];
2??:在方法中通過直接獲取注冊的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
*** * cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier" forIndexPath:indexPath];
return cell;
}
二、常用屬性
TableView視圖風格(枚舉,只讀屬性)
@property(nonatomic,readonly)UITableViewStyle? style;
設置表示圖代理和數據源代理(如果沒有設置委托代理,則代理方法中關于TableView的設置無效,新手常犯錯誤)
@property(nonatomic,assign)id <UITableViewDataSource>dataSource;
@property(nonatomic,assign)id?<UITableViewDelegate>delegate;
設置表示圖的行高(默認為44)?
注意,當使用代理方法設置高度時候,屬性設置高度會無效
@property(nonatomic)CGFloat rowHeight;
區頭和區尾的高度
(關于高度設置建議不要用屬性設置,可在對應的方法中設置:這里方法就不粘貼了)
@property(nonatomic)CGFloat ? sectionHeaderHeight;
@property(nonatomic)CGFloat ? sectionFooterHeight;
設置一個行高的估計值(默認為0,表示沒有估計,7.0之后可用),
這個屬性官方的解釋是:如果你的tableView的行高是可變的,那么設計一個估計高度可以加快代碼的運行
@property(nonatomic)CGFloat ?estimatedRowHeight;
// or
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
// return xx ? ?}
解釋:面對種類不同的 cell,我們依然可以使用簡單的 estimatedRowHeight 屬性賦值,只要整體估算值接近就可以,比如大概有一半 cell 高度是 44, 一半 cell 高度是 88, 那就可以估算一個 66,基本符合預期。
參考文檔:優化UITableViewCell高度計算的那些事
這里做cell高度的動態自適應推薦第三方庫UITableView+FDTemplateLayoutCell
設置分區頭視圖和尾視圖的估計高度和上面類似,不再贅述(7.0之后可用)
@property(nonatomic)CGFloat ?estimatedSectionHeaderHeight;
@property(nonatomic)CGFloat ?estimatedSectionFooterHeight;,
設置分割線的位置
@property(nonatomic)UIEdgeInsetsseparatorInset;
如果細心,你可能會發現系統默認的tableView的分割線左端并沒有頂到邊沿。通過這個屬性,可以手動設置分
割線的位置偏移,比如你向讓tableView的分割線只顯示右半邊,可以如下設置:
UITableView* tab = [[UITableViewalloc]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參數
決定定位的相對位置,它使一個枚舉,如下:
typedefNS_ENUM(NSInteger,UITableViewScrollPosition) {UITableViewScrollPositionNone,//同UITableViewScrollPositionTopUITableViewScrollPositionTop,//定位完成后,將定位的 顯 在tableView的頂部UITableViewScrollPositionMiddle,//定位完成后,將定位的 顯 在tableView的中間UITableViewScrollPositionBottom//定位完成后,將定位的 顯 在tableView最下
};
使表示圖定位到選中行
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
這個函數與上面的非常相似,只是它是將表示圖定位到選中的行。
四、tableView操作刷新塊的應用
注意:刷新數據顯示有延遲線程原因
[self performInMainThreadBlock:^{
[self.tableView reloadData];
}];
刷新UITableView:整個表
[self.tableView reloadData]
reloadData是刷新整個UITableView,有時候,我們可能需要局部刷新。比如:只刷新一個cell、只刷新一個section等等。這個時候在調用reloadData方法,雖然用戶看不出來,但是有些浪費資源。
刷新局部cell
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
這樣就可以很方便的刷新第一個section的第一個cell。雖然看起來代碼多了,但是確實比較節省資源。盡量少的刷新,也是UITableView的一種優化。下面是TableView涉及到有關刷新的操作,
刷新局部section
NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];進階學習[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
tableView有過表格刷新的方法
插入分區
- (void)insertSections:(NSIndexSet*)sections withRowAnimation:(UITableViewRowAnimation)animation;
animation參數是一個枚舉,枚舉的動畫類型如下
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;
我們可以將我們要做的操作全部寫在這個塊中,那么,只有當程序執行到結束快標志后,才會調用代理刷新方法。代碼示例如下:
[tabView beginUpdates];
[tabView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]]withRowAnimation:UITableViewRowAnimationLeft];[dataArray removeObjectAtIndex:1];
[tableView endUpdates];
注意:不要在這個塊中調用reloadData這個方法,它會使動畫失效。
五、tableView的編輯操作
設置是否是編輯狀態(編輯狀態下的cell左邊會出現一個減號,點擊右邊會劃出刪除按鈕)
@property(nonatomic,getter=isEditing)BOOL editing;
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
設置cell是否可以被選中(默認為YES)
@property(nonatomic)BOOL allowsSelection;
設置cell編輯模式下是否可以被選中
@property(nonatomic)BOOL allowsSelection DuringEditing;設置是否支持多選
@property(nonatomic)BOOL allowsMultipleSelection;設置編輯模式下是否支持多選
@property(nonatomic)BOOL allowsMultipleSelection DuringEditing;
六、選中cell的相關操作
獲取選中cell的位置信息
- (NSIndexPath*)indexPathForSelectedRow;
獲取多選cell的位置信息
- (NSArray*)indexPathsForSelectedRows;
代碼手動選中與取消選中某行
- (void)selectRowAtIndexPath:(NSIndexPath*)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
- (void)deselectRowAtIndexPath:(NSIndexPath*)indexPath animated:(BOOL)animated;
注意:這兩個方法將不會回調代理中的方法。
選中的cell和上一次選中的cell(單選表:選中的cell打對勾,上次選中的掛飾消失)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"你選中的%ld",(long)indexPath.row);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.tableView isEditing]) {
//如果tableView是編輯狀態-->此方法調用時機:如果你點擊的Row已經是被選中的編輯狀態,再次點擊時調用。
NSLog(@"你取消的編輯狀態行號是%ld",(long)indexPath.row);
}else{
//如果tableView不是編輯狀態:-->的到你上次點擊選中的Row。
NSLog(@"你上次選中的%ld",(long)indexPath.row);
}
}
七、tableView附件的相關方法
設置索引欄最小顯示行數
@property(nonatomic)NSIntegersectionIndexMinimumDisplayRowCount;
設置索引欄字體顏色
@property(nonatomic,retain)UIColor*sectionIndexColor;設置索引欄背景顏色
@property(nonatomic,retain)UIColor*sectionIndexBackgroundColor;設置索引欄被選中時的顏色
@property(nonatomic,retain)UIColor*sectionIndexTrackingBackgroundColor;設置分割線的風格
@property(nonatomic)UITableViewCellSeparatorStyleseparatorStyle;這個風格是一個枚舉,如下:
typedefNS_ENUM(NSInteger,UITableViewCellSeparatorStyle) {UITableViewCellSeparatorStyleNone,// 線UITableViewCellSeparatorStyleSingleLine,//有線UITableViewCellSeparatorStyleSingleLineEtched
};
設置分割線顏色
@property(nonatomic,retain)UIColor設置分割線毛玻璃效果(IOS8之后可用)
@property(nonatomic,copy)UIVisualEffect注意:這個屬性是IOS8之后新的。
*separatorColor;
*separatorEffect;
設置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(iOS6之后)
- (void)registerNib:(UINib*)nib forCellReuseIdentifier:(NSString*)identifier;通過OC類注冊cell
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString*)identifier
通過xib文件和OC類獲取注冊頭視圖和尾視圖
- (void)registerNib:(UINib*)nib forHeaderFooterViewReuseIdentifier:(NSString*)identifier;
- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString*)
更多方法參考: UITableView詳解? ? ????UITableView 總結
學無止境,做個記錄
2017-01-05 -SXH