#pragma mark someValueAboutTableView
1.tableView的樣式:UITableViewStyle
typedef NS_ENUM(NSInteger, UITableViewStyle) {
UITableViewStylePlain,? //普通類型
UITableViewStyleGrouped? // 分組類型
};
2.scrollPosition參數決定定位的相對位置
typedef NS_ENUM(NSInteger, UITableViewScrollPosition) {
UITableViewScrollPositionNone,//同UITableViewScrollPositionTop
UITableViewScrollPositionTop,//定位完成后,將定位的行顯示在tableView的頂部
UITableViewScrollPositionMiddle,//定位完成后,將定位的行顯示在tableView的中間
UITableViewScrollPositionBottom//定位完成后,將定位的行顯示在tableView最下面
};
3.行變化(插入、刪除、移動)的動畫類型
typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
UITableViewRowAnimationFade,//淡入淡出
UITableViewRowAnimationRight,//從右滑入
UITableViewRowAnimationLeft,//從左滑入
UITableViewRowAnimationTop,//從上滑入
UITableViewRowAnimationBottom,//從下滑入
UITableViewRowAnimationNone,? //沒有動畫
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100? // 自動選擇合適的動畫
};
4.
UIKIT_EXTERN NSString *const UITableViewIndexSearch NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
5.
UIKIT_EXTERN const CGFloat UITableViewAutomaticDimension NS_AVAILABLE_IOS(5_0);
#pragma mark cell側滑行為相關 UITableViewRowAction類
1.
typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) {
UITableViewRowActionStyleDefault = 0,
UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault,
UITableViewRowActionStyleNormal
} NS_ENUM_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
2.
NS_CLASS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED @interface UITableViewRowAction : NSObject
+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(nullable NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;
@property (nonatomic, readonly) UITableViewRowActionStyle style;
@property (nonatomic, copy, nullable) NSString *title;
@property (nonatomic, copy, nullable) UIColor *backgroundColor; // default background color is dependent on style
@property (nonatomic, copy, nullable) UIVisualEffect* backgroundEffect;
@end
#pragma mark UITableViewFocusUpdateContext類相關
NS_CLASS_AVAILABLE_IOS(9_0) @interface UITableViewFocusUpdateContext : UIFocusUpdateContext
@property (nonatomic, strong, readonly, nullable) NSIndexPath *previouslyFocusedIndexPath;
@property (nonatomic, strong, readonly, nullable) NSIndexPath *nextFocusedIndexPath;
@end
#pragma mark UITableViewDelegate Methods
描繪了cell的顯示和行為
一、
@optional:(選擇實現)
1.cell將要顯示時調用的方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
2.頭視圖將要顯示時調用的方法
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
3. 尾視圖將要顯示時調用的方法
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
4.和上面的方法對應,這三個方法分別是cell,頭視圖,尾視圖已經顯示時調用的方法
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
5.設置行高,頭視圖高度和尾視圖高度的方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
6.設置行高,頭視圖高度和尾視圖高度的估計值(對于高度可變的情況下,提高效率)
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
7.設置自定義頭視圖和尾視圖
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
8.
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath NS_DEPRECATED_IOS(2_0, 3_0) __TVOS_PROHIBITED;
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
9.設置cell是否可以高亮
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
10.cell高亮和取消高亮時分別調用的函數
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
11.當即將選中某行和取消選中某行時調用的函數,返回行所在分區,執行選中或者取消選中
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
12.已經選中和已經取消選中后調用的函數
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
二、編輯
1.設置tableView被編輯時的狀態風格,如果不設置,默認都是刪除風格
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
2.自定義刪除按鈕的標題
(nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
3.下面這個方法是IOS8中的新方法,用于自定義創建tableView被編輯時右邊的按鈕,按鈕類型為UITableViewRowAction。
- (nullable NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
4.設置編輯時背景是否縮進
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
5.將要編輯和結束編輯時調用的方法
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath __TVOS_PROHIBITED;
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath __TVOS_PROHIBITED;
6.移動特定的某行
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
三、行縮進
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath;
四、復制/粘貼
1.通知委托是否在指定行顯示菜單,返回值為YES時,長按顯示菜單。
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0);
2.彈出選擇菜單時會調用此方法(復制、粘貼、全選、剪切)
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender NS_AVAILABLE_IOS(5_0);
3.選擇菜單項完成之后調用此方法。
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender NS_AVAILABLE_IOS(5_0);
五、焦點
1.
- (BOOL)tableView:(UITableView *)tableView canFocusRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
2.
- (BOOL)tableView:(UITableView *)tableView shouldUpdateFocusInContext:(UITableViewFocusUpdateContext *)context NS_AVAILABLE_IOS(9_0);
3.
- (void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator NS_AVAILABLE_IOS(9_0);
4.
- (nullable NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView NS_AVAILABLE_IOS(9_0);
六、通知
1.
UITableViewSelectionDidChangeNotification
#pragma mark UITableView類
UITableView類繼承自UIScrollView
一、基礎
1.創建時必須制定類型(有普通(UITableViewStylePlain)和分組兩種類型(UITableViewStyleGrouped))
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style NS_DESIGNATED_INITIALIZER;
2.
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
3.列表視圖的類型,只讀。
@property (nonatomic, readonly) UITableViewStyle style;
4.代理
@property (nonatomic, weak, nullable) id dataSource;
@property (nonatomic, weak, nullable) id delegate;
5.高度
//行高
@property (nonatomic) CGFloat rowHeight;
//組頭的高度
@property (nonatomic) CGFloat sectionHeaderHeight;
//組尾的高度
@property (nonatomic) CGFloat sectionFooterHeight;
//估算行高,默認0
@property (nonatomic) CGFloat estimatedRowHeight NS_AVAILABLE_IOS(7_0);
//估算組頭的高度
@property (nonatomic) CGFloat estimatedSectionHeaderHeight NS_AVAILABLE_IOS(7_0);
//估算組尾的高度
@property (nonatomic) CGFloat estimatedSectionFooterHeight NS_AVAILABLE_IOS(7_0);
6.允許更改分割線的frame
@property (nonatomic) UIEdgeInsets separatorInset NS_AVAILABLE_IOS(7_0)
7.背景視圖(自動匹配tableView視圖的大小),設置后作為列表視圖(tableView)的子視圖,且在所有cell和headers/footers的后面。默認nil
@property (nonatomic, strong, nullable) UIView *backgroundView NS_AVAILABLE_IOS(3_2);
二、數據的刷新
1.刷新列表
- (void)reloadData;
2.刷新section這個方法常用語新加或者刪除了索引類別而無需刷新整個表視圖的情況下。
- (void)reloadSectionIndexTitles NS_AVAILABLE_IOS(3_0);
三、信息
1.列表的組數
@property (nonatomic, readonly) NSInteger numberOfSections;
2.某一組有多少行
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
3.某一組所占的矩形區域(包括header,footer和所有的行)
- (CGRect)rectForSection:(NSInteger)section;
4.某一組的header所占的矩形區域
- (CGRect)rectForHeaderInSection:(NSInteger)section;
5.某一組的footer所占的矩形區域
- (CGRect)rectForFooterInSection:(NSInteger)section;
6.某一分區的row所占的矩形區域
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
7.某一點在tableview上所占的分區,如果該點不在tableView的任何row上返回nil
- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
8.某一行所在的分區,如果改行是不可見的返回nil
- (nullable NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;
9.某一矩形區域內所有行所在的所有分區,返回元素為NSIndexPath類型的數組。當該矩形是一個無效值時,返回ni
- (nullable NSArray *)indexPathsForRowsInRect:(CGRect)rect;
10.某一分區的cell,如果改cell是不可見的或者indexPath超出了范圍則返回nil
- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
11.所有可見的cell,只讀數組型(數組類型為UITableViewCell),。
@property (nonatomic, readonly) NSArray<__kindof UITableViewCell *> *visibleCells;
12.所有可見行所在的分區,只讀數組型(數組類型為NSIndexPath),
@property (nonatomic, readonly, nullable) NSArray *indexPathsForVisibleRows;
13.某一組的header視圖(常用于自定義headerView的時候用)
- (nullable UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
14.某一組的footer視圖(常用于自定義footerView的時候用)
- (nullable UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
15.使表示圖定位到某一位置(行)
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
16.使表示圖定位到選中行
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
三、行的插入/刪除/刷新
1.允許多個插入/行和段被同時刪除動畫。可排序
- (void)beginUpdates;
2.只調用插入/刪除/重載呼叫或改變一更新區塊內的編輯狀態。然而對于行數等屬性可能是無效的。
- (void)endUpdates;
3.插入某些組
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
4.刪除某些組
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
5.刷新某些組
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
6.移動組section到組newSection的位置
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection NS_AVAILABLE_IOS(5_0);
7.插入某些行
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
8.刪除某些行
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
9.刷新某些分區的行
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
10.移動分區indexPath的行到分區newIndexPath
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);
四、編輯。設置之后,行的顯示會基于數據源查詢插入/刪除/重排序的控制
1.設置是否是編輯狀態(編輯狀態下的cell左邊會出現一個減號,點擊右邊會劃出刪除按鈕)
@property (nonatomic, getter=isEditing) BOOL editing;
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
2.當不在編輯模式時,是否可以選中。默認YES
@property (nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0);
3.當處在編輯模式時,是否可以選中。默認NO
@property (nonatomic) BOOL allowsSelectionDuringEditing;
4.是否可以同時選中。默認NO
@property (nonatomic) BOOL allowsMultipleSelection NS_AVAILABLE_IOS(5_0);
5.當處在編輯模式時,是否可以同時選中。默認NO
@property (nonatomic) BOOL allowsMultipleSelectionDuringEditing NS_AVAILABLE_IOS(5_0);
五、選中
1.選中的行所在的分區(單選)
@property (nonatomic, readonly, nullable) NSIndexPath *indexPathForSelectedRow;
2.選中的行所在的所有分區(多選)
@property (nonatomic, readonly, nullable) NSArray *indexPathsForSelectedRows
3.代碼手動選中與取消選中某行,注意:這兩個方法將不會回調代理中的方法。
- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
六、外觀
1.設置索引欄最小顯示行數。顯示在右側專門章節索引列表當行數達到此值。默認值為0
@property (nonatomic) NSInteger sectionIndexMinimumDisplayRowCount;
2.設置索引欄字體顏色
@property (nonatomic, strong, nullable) UIColor *sectionIndexColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
3.設置索引欄背景顏色
@property (nonatomic, strong, nullable) UIColor *sectionIndexBackgroundColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
4.設置索引欄被選中時的顏色
@property (nonatomic, strong, nullable) UIColor *sectionIndexTrackingBackgroundColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
5.設置分割線的風格
@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle __TVOS_PROHIBITED;
6.設置分割線顏色
@property (nonatomic, strong, nullable) UIColor *separatorColor UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
7.設置分割線毛玻璃效果(IOS8之后可用)
@property (nonatomic, copy, nullable) UIVisualEffect *separatorEffect NS_AVAILABLE_IOS(8_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
8.
@property (nonatomic) BOOL cellLayoutMarginsFollowReadableWidth NS_AVAILABLE_IOS(9_0);
9.設置tableView頭視圖
@property (nonatomic, strong, nullable) UIView *tableHeaderView;
10.設置tableView尾視圖
@property (nonatomic, strong, nullable) UIView *tableFooterView;
11.從復用池中取cell
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;
12.獲取一個已注冊的cell
- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
13.從復用池獲取頭視圖或尾視圖
- (nullable __kindof UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
14.通過xib文件注冊cell
- (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
15.通過oc類注冊cell
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
16.通過xib文件注冊頭視圖和尾視圖
- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
17.通過OC類注冊頭視圖和尾視圖
- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
18.
@property (nonatomic) BOOL remembersLastFocusedIndexPath NS_AVAILABLE_IOS(9_0);
#pragma mark UITableViewDataSource Methods
數據源協議方法,這個協議描繪了數據源模型,它不提供關于外觀的任何信息(包括cell)
@required:(必須實現)
1.每一組有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
2.行顯示,可以通過每個cell的reuseIdentifier對多種多樣的cell進行查找,進而進行cell的復用。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@optional:(選擇實現)
一、基本
1. 列表有多少組(默認1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
2.組頭標題,字體樣式是固定的。如果想要不同的樣式,可以自定義
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
3.組底標題,字體樣式是固定的。如果想要不同的樣式,可以自定義
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
二、編輯相關
1.每一行可以設置自己的編輯屬性,默認YES,即使否可以刪除移動選中等。
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
三、移動/重新排序
1.設置某行是否可以被移動
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
2.設置索引欄標題數組(實現這個方法,會在tableView右邊顯示每個分區的索引),例如:ABCDEFG...Z
- (nullable NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView __TVOS_PROHIBITED;
3.設置索引欄標題對應的分區
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index __TVOS_PROHIBITED;
4.tableView接受編輯時調用的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
5.tableView的cell被移動時調用的方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
#pragma mark NSIndexPath (UITableView)
1.類方法
+ (instancetype)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;
2.indexPath的組
@property (nonatomic, readonly) NSInteger section;
3.indexPath的行
@property (nonatomic, readonly) NSInteger row;