- 需遵照協議 UITableViewDataSource, UITableViewDelegate,並設置代理
UITableViewDelegate 繼承自 UIScrollViewDelegate
@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
一、UITableViewDataSource 和 UITableViewDelegate 協議方法
// 設置分段數,設置 tableView 有多少個分段
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return myDataArray.count;
}
// 設置行數,設置 tableView 中每段中有多少行,section 就是第幾分段
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[myDataArray objectAtIndex:section] count];
}
// 設置行高 ,默認爲 44
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60;
}
// 設置估計行高
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
/*
只要返回了估計高度,那麼就會先調用 tableView:cellForRowAtIndexPath: 方法建立 cell,
再調用 tableView:heightForRowAtIndexPath: 方法獲取 cell 的真實高度,
而且顯示一個 cell,調用一次 tableView:heightForRowAtIndexPath: 方法。
若是不返回估計高度,會先調用 tableView:heightForRowAtIndexPath: 方法,
再調用 tableView:heightForRowAtIndexPath: 方法,
而且一次性所有調用總 cell 數量次 tableView:heightForRowAtIndexPath: 方法。
*/
return 60;
}
// 設置每一行顯示的內容,每當有一個 cell 進入視野範圍內就會調用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return cell;
}
// 設置分段的頭標題高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
// 設置分段的腳標題高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 30;
}
// 設置分段的頭標題估計高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {
return 40;
}
// 設置分段的腳標題估計高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section {
return 30;
}
// 設置分段的頭標題內容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (0 == section) {
return @"1 Header";
}
else{
return @"2 rHeader";
}
}
// 設置分段的腳標題內容
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
if (0 == section) {
return @"2 Footer";
}
else{
return @"2 Footer";
}
}
// 設置分段頭標題視圖,返回自定義的標題視圖
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return myView;
}
// 設置分段腳標題視圖,返回自定義的標題視圖
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return myView;
}
// 建立索引條
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return array;
}
// 設置索引條偏移量,默認索引條與分段一一對應時,能夠不寫該方法
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
/*
點擊索引條上字符串的時候 tableView 會跳轉到對應的分段,是根據位置計算的,
點擊索引條上第幾個,tableView 就會跳到第幾段。
若是索引條的前面加了個搜索小圖標等,須要重寫這個方法。
*/
}
// 表格選中點擊響應事件,表格被選中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
// 表格取消選中點擊響應事件,表格被取消選中
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
}
// 附屬控件 button 點擊響應事件
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
// 若是系統自帶的附屬控件裏有 button ,附屬控件的點擊事件會獨立出來
}
// 表格刪除、插入
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// 表格刪除或插入,默認爲刪除模式,寫入該方法即表示容許刪除。
}
// 設置編輯模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// 刪除、插入、多選刪除,不設置默認時爲刪除
}
// 修改左滑刪除按鈕的內容
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"刪除";
}
// 設置左滑多按鈕
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
// 按鈕從右向左的順序排列
return @[action1, action0];
}
// 表格移動
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
}
二、UIScrollViewDelegate 協議方法
// 將要開始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
}
// 將要結束拖拽
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
}
// 已經結束拖拽,decelerate 鬆手後 是否有慣性滾動 0:沒有,1:有
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
}
// 滾動過程當中,只要滾動就會觸發
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
}
// 已經結束滾動,滾動動畫中止時執行,代碼改變時觸發,也就是 setContentOffset 改變時
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
}
// 將要開始慣性滾動
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
}
// 已經結束慣性滾動
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
}
// 設置點擊狀態欄時是否滾到頂端
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
return YES;
}
// 已經滾到頂端,點擊狀態欄時調用
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
}
// 設置被縮放的空間,一個 scrollView 中只能有一個子控件被縮放,若是有不少個子控件縮放時會引發錯亂
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [scrollView.subviews[0] viewWithTag:100];
}
// 將要開始縮放
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
}
// 已經結束縮放
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
}
// 縮放過程當中,只要縮放就會觸發
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
}