對(duì)API中的UITableView的屬性三圆、方法以及使用的記錄,如果有什么不準(zhǔn)確的地方避咆,歡迎大家來(lái)指出舟肉、補(bǔ)充。
更新一篇UITableView的API記錄查库,之后會(huì)陸續(xù)發(fā)一些其他類的API記錄路媚。代碼鏈接在本文最下方,歡迎Star或者Fark樊销。
//
// UITableView.h
// UIKit
//
// Copyright (c) 2005-2015 Apple Inc. All rights reserved.
//
// Coder_Sun的GitHub地址:https://github.com/272095249
// Coder_Sun簡(jiǎn)書網(wǎng)址:http://www.reibang.com/u/87c7aa9de064
//
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIScrollView.h>
#import <UIKit/UISwipeGestureRecognizer.h>
#import <UIKit/UITableViewCell.h>
#import <UIKit/UIKitDefines.h>
NS_ASSUME_NONNULL_BEGIN
UITableViewStyle
typedef NS_ENUM(NSInteger, UITableViewStyle) {
UITableViewStylePlain, // 普通類型
UITableViewStyleGrouped // 分組類型
};
scrollPosition參數(shù)決定定位的相對(duì)位置
typedef NS_ENUM(NSInteger, UITableViewScrollPosition) {
UITableViewScrollPositionNone, //同UITableViewScrollPositionTop
UITableViewScrollPositionTop, //定位完成后整慎,將定位的行顯示在tableView的頂部
UITableViewScrollPositionMiddle, //定位完成后,將定位的行顯示在tableView的中間
UITableViewScrollPositionBottom //定位完成后围苫,將定位的行顯示在tableView的最下面
};
// scroll so row of interest is completely visible at top/center/bottom of view
行變化(插入裤园、刪除、移動(dòng)的動(dòng)畫類型)
typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
UITableViewRowAnimationFade, // 淡入淡出
UITableViewRowAnimationRight, // 從右滑入
UITableViewRowAnimationLeft, // 從左滑入
UITableViewRowAnimationTop, // 從上滑入
UITableViewRowAnimationBottom, // 從下滑入
UITableViewRowAnimationNone, // 沒有動(dòng)畫
UITableViewRowAnimationMiddle, // 從中間出來(lái)
UITableViewRowAnimationAutomatic = 100 // 自動(dòng)選擇合適的動(dòng)畫
};
// 擴(kuò)大鏡(一般放到第一個(gè))
UIKIT_EXTERN NSString *const UITableViewIndexSearch NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
// Returning this value from tableView:heightForHeaderInSection: or tableView:heightForFooterInSection: results in a height that fits the value returned from
// tableView:titleForHeaderInSection: or tableView:titleForFooterInSection: if the title is not nil.
UIKIT_EXTERN const CGFloat UITableViewAutomaticDimension NS_AVAILABLE_IOS(5_0);
@class UITableView;
@class UINib;
@protocol UITableViewDataSource;
@class UILongPressGestureRecognizer;
@class UITableViewHeaderFooterView;
@class UIRefreshControl;
@class UIVisualEffect;
應(yīng)用于動(dòng)作按鈕的樣式
typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) {
UITableViewRowActionStyleDefault = 0, // 將默認(rèn)樣式應(yīng)用于按鈕
UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault, // 等于默認(rèn)樣式
UITableViewRowActionStyleNormal // 應(yīng)用反應(yīng)標(biāo)準(zhǔn)非破壞性操作的風(fēng)格
} NS_ENUM_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
NS_CLASS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED@interface UITableViewRowAction : NSObject <NSCopying>
// 便利初始化方法:
+ (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;// 默認(rèn)背景顏色
@property (nonatomic,copy, nullable) UIVisualEffect* backgroundEffect;
@end
NS_CLASS_AVAILABLE_IOS(9_0)@interface UITableViewFocusUpdateContext : UIFocusUpdateContext
@property (nonatomic,strong, readonly,nullable) NSIndexPath *previouslyFocusedIndexPath;
@property (nonatomic,strong, readonly,nullable) NSIndexPath *nextFocusedIndexPath;
@end
———————————————分割線—————————————————
UITableViewDelegate
@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
@optional
顯示自定義
// cell將要顯示時(shí)回調(diào)
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
// section的header將要顯示時(shí)回調(diào)
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
// section的fotter將要顯示時(shí)回調(diào)
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
// cell已顯示時(shí)回調(diào)
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
// section的header已顯示時(shí)回調(diào)
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
// section的fotter已顯示時(shí)回調(diào)
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
可變高度支持
// 返回行高(這里高度通過(guò)協(xié)議返回剂府,是為了table能準(zhǔn)確的定位出來(lái)要顯示的Cell-index拧揽,從而滿足UITableView的重用機(jī)制)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
// 返回section的header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
// 返回section的footer的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
// Use the estimatedHeight methods to quickly calcuate guessed values which will allow for fast load times of the table.
// If these methods are implemented, the above -tableView:heightForXXX calls will be deferred until views are ready to be displayed, so more expensive logic can be placed there.
// 設(shè)置行高,頭視圖高度和尾視圖高度的估計(jì)值(對(duì)于高度可變的情況下腺占,提高效率)
// [https://www.shinobicontrols.com/blog/ios7-day-by-day-day-19-uitableview-row-height-estimation](https://www.shinobicontrols.com/blog/ios7-day-by-day-day-19-uitableview-row-height-estimation) //tableView估算高度網(wǎng)址
// 返回預(yù)估的行高
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);
// 返回預(yù)估section的header高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
// 返回預(yù)估section的footer高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
/*
使用:
self.tableView.rowHeight=UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight=44.0;
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
在 cellforrow里調(diào)用 [cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
要結(jié)合起來(lái)我之前用Storybord做cell自使用 就使用這個(gè)方法
*/
// Section header & footer information. Views are preferred over title should you decide to provide both
// 返回第section組頭部控件 (會(huì)調(diào)整默認(rèn)活已經(jīng)指定的header的高度)
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; // custom view for header. will be adjusted to default or specified header height
// 返回第section組尾部控件 (會(huì)調(diào)整默認(rèn)活已經(jīng)指定的footer的高度)
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; // custom view for footer. will be adjusted to default or specified footer height
Accessories (disclosures).
// 當(dāng)cell的accessaryType為UITableViewCellAccessoryFetailDisclosureButton時(shí)淤袜,點(diǎn)擊accessaryView將會(huì)調(diào)用delegate的tableView:accessoryButtonTappedForRowWithIndexPath方法。否則只只是didSelectRowAtIndexPath湾笛; (accessaryView輔助視圖)
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath NS_DEPRECATED_IOS(2_0,3_0) __TVOS_PROHIBITED;
// 設(shè)置每個(gè)單元格上面的按鈕的點(diǎn)擊方法
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
Selection
// 當(dāng)前選中的row是否高亮
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
// 指定row高亮
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
// 通知委托表視圖的指定行不在高亮顯示饮怯,一般是點(diǎn)擊其他行的時(shí)候
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
// cell選擇
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
// cell取消選擇
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
// 已經(jīng)選擇選中后調(diào)用的函數(shù)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
// 已經(jīng)取消選擇選中后調(diào)用的函數(shù)
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
/* 先點(diǎn)擊row1,再點(diǎn)擊row2嚎研,兩者執(zhí)行順序:在下一行將要選中后才取消上一行的選中
willSelectRowAtIndexPath 當(dāng)前row為:0
didSelectRowAtIndexPath 當(dāng)前row為:0
willSelectRowAtIndexPath 當(dāng)前row為:1
willDeselectRowAtIndexPath 當(dāng)前row為:0
didDeselectRowAtIndexPath 當(dāng)前row為:0
didSelectRowAtIndexPath 當(dāng)前row為:1
*/
Editing 編輯
// 設(shè)置tableView被編輯時(shí)的狀態(tài)風(fēng)格蓖墅,如果不設(shè)置库倘,默認(rèn)是刪除風(fēng)格
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
// 自定義刪除按鈕的標(biāo)題
- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
// 用于自定義創(chuàng)建tableView被編輯時(shí)右邊的按鈕,按鈕類型為UITableViewRowAction
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED; // supercedes -tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: if return value is non-nil
// 設(shè)置編輯模式下是否需要對(duì)表視圖指定行進(jìn)行縮進(jìn)论矾,NO為關(guān)閉縮進(jìn)教翩,這個(gè)方法可以用來(lái)去掉move時(shí)row前面的空白
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
// 開始編輯前調(diào)用
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath __TVOS_PROHIBITED;
// 完成編輯后調(diào)用
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath __TVOS_PROHIBITED;
Moving/reordering
// 移動(dòng)特定的某行(注意區(qū)別之前的tableView:moveRowAtIndexPath:toIndexPath方法。當(dāng)手指按住reorde accessory view移動(dòng)時(shí)贪壳,只要有row moved/reordered都會(huì)調(diào)用該方法饱亿,而前者方法只有當(dāng)手指放開reorder accessory view時(shí),結(jié)束move/order操作才會(huì)調(diào)用自己闰靴。返回值代表進(jìn)行移動(dòng)操作后回到的行彪笼,如果設(shè)置為當(dāng)前行,則不論怎么移動(dòng)都會(huì)回到當(dāng)前行)
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
Indentation(縮進(jìn))
// 行縮進(jìn)
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath;// return 'depth' of row for hierarchies
// 長(zhǎng)按出來(lái)的Copy/Paste操作(復(fù)制粘貼)----->通知委托是否在指定行顯示菜單蚂且,返回值為YES時(shí)配猫,長(zhǎng)按顯示菜單
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0);
// 彈出選擇菜單時(shí)會(huì)調(diào)用此方法(復(fù)制、粘貼杏死、全選泵肄、剪切)
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullableid)sender NS_AVAILABLE_IOS(5_0);
// 當(dāng)用戶選擇菜單中的某個(gè)選項(xiàng)時(shí)調(diào)用
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullableid)sender NS_AVAILABLE_IOS(5_0);
Focus(焦點(diǎn))
// 返回能否獲得焦點(diǎn)
- (BOOL)tableView:(UITableView *)tableView canFocusRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
// 返回是否將要更新焦點(diǎn)
- (BOOL)tableView:(UITableView *)tableView shouldUpdateFocusInContext:(UITableViewFocusUpdateContext *)context NS_AVAILABLE_IOS(9_0);
// 已經(jīng)更新焦點(diǎn)時(shí)調(diào)用
- (void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator NS_AVAILABLE_IOS(9_0);
// 返回上一個(gè)焦點(diǎn)的indexPath
- (nullable NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView NS_AVAILABLE_IOS(9_0);
@end
———————————————分割線—————————————————
UITableViewSelectionDidChangeNotification(通知)
UIKIT_EXTERN NSString *const UITableViewSelectionDidChangeNotification; // 通知
NS_CLASS_AVAILABLE_IOS(2_0)@interface UITableView : UIScrollView <NSCoding>
// 便利初始化方式:根據(jù)風(fēng)格
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style NS_DESIGNATED_INITIALIZER;// must specify style at creation. -initWithFrame: calls this with UITableViewStylePlain
- (nullableinstancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
@property (nonatomic,readonly) UITableViewStyle style; // 列表視圖的類型,只讀
@property (nonatomic,weak, nullable)id <UITableViewDataSource> dataSource;
@property (nonatomic,weak, nullable)id <UITableViewDelegate> delegate;
@property (nonatomic) CGFloat rowHeight; // 行高
@property (nonatomic) CGFloat sectionHeaderHeight; // 組頭的高度
@property (nonatomic) CGFloat sectionFooterHeight; // 組尾的高度
@property (nonatomic) CGFloat estimatedRowHeight NS_AVAILABLE_IOS(7_0); // 估算行高淑翼,默認(rèn)0
@property (nonatomic) CGFloat estimatedSectionHeaderHeight NS_AVAILABLE_IOS(7_0); // 估算組頭的高度
@property (nonatomic) CGFloat estimatedSectionFooterHeight NS_AVAILABLE_IOS(7_0); // 估算組尾的高度
@property (nonatomic) UIEdgeInsets separatorInset NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;// 允許更改分割線的frame
@property (nonatomic,strong, nullable) UIView *backgroundView NS_AVAILABLE_IOS(3_2);// 背景視圖(自動(dòng)匹配tableView視圖大懈病),設(shè)置互作為列表視圖的子視圖玄括,切在所有cell和headers/footers的后面冯丙,默認(rèn)為nil
Data(數(shù)據(jù)的刷新)
- (void)reloadData;// 刷新列表
- (void)reloadSectionIndexTitles NS_AVAILABLE_IOS(3_0); // 刷新你section這個(gè)方法常用語(yǔ)新加或者刪除了索引類別二無(wú)需率先呢整個(gè)表視圖的情況下
Info(信息)
@property (nonatomic,readonly) NSInteger numberOfSections; // 列表的組數(shù)
// 某一組有多少行
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
// 某一組所占的矩形區(qū)域(包括header,footer和所有的行)
- (CGRect)rectForSection:(NSInteger)section;
// 某一組的header所占的矩形區(qū)域
- (CGRect)rectForHeaderInSection:(NSInteger)section;
// 某一組的footer所占的矩形區(qū)域
- (CGRect)rectForFooterInSection:(NSInteger)section;
// 某一分區(qū)的row所占的矩形區(qū)域
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
// 某一點(diǎn)在tableView上所占的分區(qū)惠豺,如果該點(diǎn)不在tableView的任何row上返回nil
- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
// 某一行所在的分區(qū)银还,如果改行是不可見的返回nil
- (nullable NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;
// 某一矩形區(qū)域內(nèi)所有行所在的所有分區(qū),返回元素為NSIndexPath類型的數(shù)組洁墙。當(dāng)該矩形是一個(gè)無(wú)效值時(shí)蛹疯,返回nil
- (nullable NSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect;
// 某一分區(qū)的cell沒如果改cell是不可見的或者indexPath超出了返回則返回nil
- (nullable__kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@property (nonatomic,readonly) NSArray<__kindof UITableViewCell *> *visibleCells; // 所有可見的cell,只讀數(shù)組型(數(shù)組類型為UITableViewCell)
@property (nonatomic,readonly, nullable) NSArray<NSIndexPath *> *indexPathsForVisibleRows; // 所有可見行所在的分區(qū)热监,只讀數(shù)組型(NSIndexPath)
// 某一組的header視圖(常用語(yǔ)自定義headerView用)
- (nullable UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
// 某一組的footer視圖(常用語(yǔ)自定義footerView用)
- (nullable UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
//使表視圖定位到某一位置(行)
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
// 使表視圖定位到選中行
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
Row insertion/deletion/reloading.
// 這兩個(gè)方法捺弦,是配合起來(lái)使用的,標(biāo)記了一個(gè)tableView的動(dòng)畫快孝扛。分別代表動(dòng)畫的開始和結(jié)束列吼。兩者成對(duì)出現(xiàn),可以嵌套使用苦始。一般寞钥,在添加,刪除陌选,選擇tableView中使用理郑,并實(shí)現(xiàn)動(dòng)畫效果蹄溉。在動(dòng)畫快內(nèi),不建議使用reloadData方法您炉,如果使用柒爵,會(huì)影響動(dòng)畫
- (void)beginUpdates; // 允許多個(gè)插入/行和段被同時(shí)刪除動(dòng)畫∽簦可排序
- (void)endUpdates; // 只調(diào)用插入/刪除/重載呼叫或改變一更新區(qū)塊內(nèi)的編輯狀態(tài)棉胀。然而對(duì)于行數(shù)等屬性可能是無(wú)效的
// 插入某些組
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
// 刪除某些組
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
// 刷新某些組
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
// 一定組section到組newSection的位置
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection NS_AVAILABLE_IOS(5_0);
// 插入某些行
- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
// 刪除某些行
- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
// 刷新tableView指定行的數(shù)據(jù)
- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
// 移動(dòng)分區(qū)indexPath的行到分區(qū)newIndexPath
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);
// 編輯、設(shè)置之后冀膝,行的顯示會(huì)基于數(shù)據(jù)源查詢插入/刪除/重排序的控制
@property (nonatomic,getter=isEditing)BOOL editing; // 設(shè)置是否是編輯狀態(tài)(編輯狀態(tài)下的cell左邊會(huì)出現(xiàn)一個(gè)減號(hào)唁奢,編輯右邊會(huì)劃出刪除按鈕)
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
@property (nonatomic)BOOL allowsSelection NS_AVAILABLE_IOS(3_0); // 當(dāng)不再編輯模式時(shí),是否可以選中畸写,默認(rèn)YES
@property (nonatomic)BOOL allowsSelectionDuringEditing; // 當(dāng)處在編輯模式時(shí)驮瞧,是否可以選中氓扛。默認(rèn)NO
@property (nonatomic)BOOL allowsMultipleSelection NS_AVAILABLE_IOS(5_0); // 是否可以同時(shí)選中枯芬。默認(rèn)NO
@property (nonatomic)BOOL allowsMultipleSelectionDuringEditing NS_AVAILABLE_IOS(5_0); // 當(dāng)處在編輯模式時(shí),是否可以同時(shí)選中采郎。默認(rèn)NO
Selection(選中)
@property (nonatomic,readonly, nullable) NSIndexPath *indexPathForSelectedRow;// 選中的行所在的分區(qū)(單選)
@property (nonatomic,readonly, nullable) NSArray<NSIndexPath *> *indexPathsForSelectedRows NS_AVAILABLE_IOS(5_0);// 選中的行所在的所有分區(qū)(多選)
// Selects and deselects rows. These methods will not call the delegate methods (-tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath:), nor will it send out a notification.
// 選中某行千所,注意:這兩個(gè)方法將不會(huì)回調(diào)代理中的方法
- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
// 取消選中某行
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
Appearance(外觀)
@property (nonatomic) NSInteger sectionIndexMinimumDisplayRowCount; // 設(shè)置索引欄最小顯示行數(shù)。先睡先在右側(cè)專門章節(jié)索引列表當(dāng)行數(shù)達(dá)到此值蒜埋。默認(rèn)值是0
@property (nonatomic,strong, nullable) UIColor *sectionIndexColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR; // 設(shè)置索引欄字體顏色
@property (nonatomic,strong, nullable) UIColor *sectionIndexBackgroundColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; // 設(shè)置索引欄背景顏色
@property (nonatomic,strong, nullable) UIColor *sectionIndexTrackingBackgroundColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR; // 設(shè)置索引欄被選中時(shí)的顏色
@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle __TVOS_PROHIBITED; // 設(shè)置分割線的風(fēng)格
@property (nonatomic,strong, nullable) UIColor *separatorColor UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED; // 設(shè)置分割線顏色
@property (nonatomic,copy, nullable) UIVisualEffect *separatorEffect NS_AVAILABLE_IOS(8_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED; // 設(shè)置分割線毛玻璃效果(iOS8之后可用)
@property (nonatomic)BOOL cellLayoutMarginsFollowReadableWidth NS_AVAILABLE_IOS(9_0); // 判斷是否需要根據(jù)內(nèi)容留有空白
@property (nonatomic,strong, nullable) UIView *tableHeaderView; // 設(shè)置tableView頭視圖
@property (nonatomic,strong, nullable) UIView *tableFooterView; // 設(shè)置tableView尾視圖
// 從復(fù)用池張取cell
- (nullable__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;
// 獲取一個(gè)已注冊(cè)的cell
- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
// 從復(fù)用池獲取頭視圖或尾視圖
- (nullable__kindof UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
// 通過(guò)xib文件注冊(cè)cell
- (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
// 通過(guò)oc類注冊(cè)cell
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
// 通過(guò)xib文件注冊(cè)頭視圖和尾視圖
- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
// 通過(guò)OC類注冊(cè)頭視圖和尾視圖
- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
Focus
@property (nonatomic)BOOL remembersLastFocusedIndexPath NS_AVAILABLE_IOS(9_0); // 使用Apple TV遙控器控制屏幕上的用戶界面
@end
———————————————分割線—————————————————
UITableViewDataSource
// this protocol represents the data model object. as such, it supplies no information about appearance (including the cells)
@protocol UITableViewDataSource<NSObject>
@required
// 每個(gè)section下cell的個(gè)數(shù)(必須實(shí)現(xiàn))
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
// 通過(guò)Indexpath返回具體的cell(必須實(shí)現(xiàn))
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@optional
// 返回有多少個(gè)section(默認(rèn)是1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
// 每個(gè)section上面的標(biāo)語(yǔ)內(nèi)容
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
// 每個(gè)section下面的標(biāo)語(yǔ)內(nèi)容
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
Editing
// Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
// 是否可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
Moving/reordering
// Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:
// 是否可拖拽
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
Index
// 右側(cè)索引條需要的數(shù)組內(nèi)容
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView __TVOS_PROHIBITED; // return list of section titles to display in section index view (e.g. "ABCD...Z#")
// 索引值對(duì)應(yīng)的section-index
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index __TVOS_PROHIBITED; // tell table which section corresponds to section title/index (e.g. "B",1))
Data manipulation - insert and delete support
// 對(duì)Cell編輯后的回調(diào)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
// 對(duì)Cell拖拽后的回調(diào)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
@end
———————————————分割線—————————————————
NSIndexPath
// This category provides convenience methods to make it easier to use an NSIndexPath to represent a section and row
@interface NSIndexPath (UITableView)
// 類方法
+ (instancetype)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;
@property (nonatomic,readonly) NSInteger section; // indexPath的組
@property (nonatomic,readonly) NSInteger row; // indexPath的行
@end
NS_ASSUME_NONNULL_END
———————————————分割線—————————————————
dataSource/delegate方法大概執(zhí)行順序(所有方法均實(shí)現(xiàn))
/*
----dataSource/delegate方法大概執(zhí)行順序(所有方法均實(shí)現(xiàn)):
1).numberOfSectionsInTableView淫痰;有多少section,例如k整份;
2).tableView:estimatedHeightForHeaderInSection + tableView:estimatedHeightForFooterInSection待错;計(jì)算k-1 section的header、footer大概高度烈评;
3).tableView:numberOfRowsInSection火俄;k-1 section有多少 row;
4).tableView:estimatedHeightForRowAtIndexPath讲冠;計(jì)算k-1 section中所有row的大概高度瓜客;
5).重復(fù)1)~4)步驟,直到所有0至k-1的section計(jì)算完竿开;
6).sectionIndexTitlesForTableView谱仪;索引titles;
7).tableView:heightForRowAtIndexPath否彩;依次計(jì)算visible區(qū)域(屏幕區(qū)域)里每個(gè)cell的高度(這里的所有cell記做集合A疯攒,決定于屏幕高度和estimatedHeightForXXX方法)
8).tableView:cellForRowAtIndexPath;創(chuàng)建第一個(gè)indexPath上的cell
9).tableView:indentationLevelForRowAtIndexPath列荔; indexPath上的cell的縮進(jìn)敬尺;
10).tableView:canEditRowAtIndexPath称杨; indexPath上的cell編輯屬性;
11).tableView:willDisplayCell:forRowAtIndexPath筷转; indexPath上的cell將要顯示姑原;
12),重復(fù)8)~11),直到所有visible區(qū)域cell(集合A)創(chuàng)建完畢呜舒;
13).tableView:heightForHeaderInSection + tableView:heightForFooterInSection + tableView:viewForHeaderInSection + tableView:viewForHeaderInSection锭汛;依次計(jì)算visible區(qū)域里所有section的header高度、footer高度袭蝗、viewForHead唤殴、viewForFooter;
執(zhí)行順序(沒有實(shí)現(xiàn)estimatedHeight這些方法)
----執(zhí)行順序(沒有實(shí)現(xiàn)estimatedHeight這些方法):
1).numberOfSectionsInTableView到腥;有多少section朵逝,例如k;
2).tableView:heightForHeaderInSection + tableView:heightForFooterInSection乡范;計(jì)算k-1 section的header配名、footer高度;
3).tableView:numberOfRowsInSection晋辆;k-1 section有多少 row渠脉;
4).tableView:heightForRowAtIndexPath;計(jì)算k-1 section中所有row得高度瓶佳;
5).重復(fù)1)~4)步驟芋膘,直到所有0至k-1的section計(jì)算完
6).sectionIndexTitlesForTableView;索引titles霸饲;
7).tableView:cellForRowAtIndexPath为朋;創(chuàng)建第一個(gè)indexPath上的cell
8).tableView:indentationLevelForRowAtIndexPath; indexPath上的cell的縮進(jìn)厚脉;
9).tableView:canEditRowAtIndexPath习寸; indexPath上的cell編輯屬性;
10).tableView:willDisplayCell:forRowAtIndexPath器仗; indexPath上的cell將要顯示融涣;
12).重復(fù)7)~12),知道所有visible區(qū)域(屏幕)cell創(chuàng)建完畢精钮;
13).tableView:viewForHeaderInSection + tableView:viewForHeaderInSection威鹿;依次計(jì)算visible區(qū)域里所有的viewForHead、viewForFooter轨香;
備注
備注:
1.由上可看出忽你,estimatedHeight在加載tableview的時(shí)候代替了heightFor方法,heightFor方法只有當(dāng)cell需要顯示的時(shí)候臂容,才會(huì)調(diào)用科雳。
2.關(guān)于estimatedHeightForXXX相關(guān)方法根蟹,里面的返回值并不能隨意填寫的,應(yīng)該是真實(shí)高度的大概值糟秘;因?yàn)樵诩虞dtableview的時(shí)候简逮,當(dāng)所有的section header/footer row的高度都大概計(jì)算完,開始計(jì)算高度尿赚、并創(chuàng)建visible區(qū)域的cell時(shí)候散庶,這些cell屬不屬于visible區(qū)域的判斷依據(jù)就是之前的estimatedHeightForXXX方法返回的值算出來(lái)的;例如estimatedHeightForXXX相關(guān)方法返回值過(guò)大凌净,算出來(lái)當(dāng)前visible(屏幕)區(qū)域悲龟,包含3個(gè)section header 、footer以及里面的row冰寻,所以實(shí)際創(chuàng)建的時(shí)候也是創(chuàng)建這些cell(參考上文中方法執(zhí)行順序)须教,當(dāng)這些cell創(chuàng)建完,實(shí)際情況高度(heightForXXX方法所得)可能只占visible(屏幕)區(qū)域的一半斩芭,導(dǎo)致屏幕另一半空白轻腺。注意visible區(qū)域初始顯示的cell是由estimatedHeightForXXX相關(guān)方法決定的,而不是heightForXXX這些方法真實(shí)高度決定的秒旋,所以有時(shí)tableview中visible區(qū)域尾部cell顯示不出來(lái)或者創(chuàng)建的cell比visible區(qū)域cell多约计,都是estimatedHeightForXXX和heightForXXX方法相差導(dǎo)致的原因。
3.以上方法和ViewController那些方法關(guān)系:先執(zhí)行viewdidload迁筛、willAppear等相關(guān)方法,再執(zhí)行numberOfSectionsInTableView系列方法耕挨。
*/