您的位置:首页 > 产品设计 > UI/UE

iOS开-UIKit( UITableView.h - -解读)

2015-12-08 09:11 417 查看
// UITableView.h

#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

typedef NS_ENUM(NSInteger, UITableViewStyle) {

UITableViewStylePlain, // regular table view

UITableViewStyleGrouped // preferences style table view

};

typedef NS_ENUM(NSInteger, UITableViewScrollPosition) {

UITableViewScrollPositionNone,

UITableViewScrollPositionTop,

UITableViewScrollPositionMiddle,

UITableViewScrollPositionBottom

}; // scroll so row of interest is completely visible at top/center/bottom of view

typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {

UITableViewRowAnimationFade,

UITableViewRowAnimationRight, // slide in from right (or out to right)

UITableViewRowAnimationLeft,

UITableViewRowAnimationTop,

UITableViewRowAnimationBottom,

UITableViewRowAnimationNone, // available in iOS 3.0

UITableViewRowAnimationMiddle, // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupy

UITableViewRowAnimationAutomatic = 100 // available in iOS 5.0. chooses an appropriate animation style for you

};

// Including this constant string in the array of strings returned by sectionIndexTitlesForTableView: will cause a magnifying glass icon to be displayed at that location in the index.

// This should generally only be used as the first title in the index.

UIKIT_EXTERN NSString *const UITableViewIndexSearchNS_AVAILABLE_IOS(3_0);

// 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 constCGFloat UITableViewAutomaticDimension
NS_AVAILABLE_IOS(5_0);

@class UITableView;

@class UINib;

@protocol UITableViewDataSource;

@class UILongPressGestureRecognizer;

@class UITableViewHeaderFooterView;

@class UIRefreshControl;

@class UIVisualEffect;

typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) {

UITableViewRowActionStyleDefault = 0,

UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault,

UITableViewRowActionStyleNormal

} NS_ENUM_AVAILABLE_IOS(8_0);

NS_CLASS_AVAILABLE_IOS(8_0)@interface UITableViewRowAction :
NSObject <NSCopying>

+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(nullableNSString *)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

//**************************************************************

//这个协议描述了cell的显示和行为

@protocol UITableViewDelegate<NSObject,UIScrollViewDelegate>

@optional

//即将显示indexPath处的cell时触发

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

//即将显示section处的Header时触发

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);

//即将显示section处的Footer时触发

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);

//结束显示indexPath处的cell时触发

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPathNS_AVAILABLE_IOS(6_0);

//结束显示section处的Header时触发

- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);

//结束显示section处的Footer时触发

- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);

//该方法返回值决定指定indexPath对应的row的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

//该方法返回值决定指定isection对应的Header的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

//该方法返回值决定指定section对应的Footer的高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

//设置一个行高的估计值(默认为0,表示没有估计,7.0之后可用)

注意:这个属性官方的解释是如果你的tableView的行高是可变的,那么设计一个估计高度可以加快代码的运行效率。

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(7_0);

//下面这两个属性和上面相似,分别设置分区头视图和尾视图的估计高度(7.0之后可用)

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)sectionNS_AVAILABLE_IOS(7_0);

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)sectionNS_AVAILABLE_IOS(7_0);

//该方法返回值决定指定isection对应的Header视图

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

//该方法返回值决定指定section对应的Footer视图

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

//已弃用

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPathNS_DEPRECATED_IOS(2_0,3_0);

//accessoryButton的点击事件

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;

// Cell高亮的回调,一般式在选择的时候才高亮

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(6_0);

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(6_0);

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(6_0);

// Cell选中和取消选择的回调

- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;

- (nullable NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(3_0);

// Called after the user changes the selection.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(3_0);

//该方法返回值决定了 indexPath处的cell的编辑状态
返回值为枚举类型
分别为 None Delete Insert

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

//删除按钮上的文字

- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath
*)indexPath NS_AVAILABLE_IOS(3_0);

//返回一个UITableViewRowAction数组,每一个"Action"代表一个侧滑删除的Button。这样侧滑每一行Cell可以有更多按钮提供给用户交互。

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath
*)indexPath NS_AVAILABLE_IOS(8_0);

//该方法决定了 cell处于被编辑状态时是否应该缩进 若未重写
所有cell处于编辑状态时都会缩进

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;

// The willBegin/didEnd methods are called whenever the 'editing' property is automatically changed by the table (allowing insert/delete/move). This is done by a swipe activating a single row

- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath;

// Moving/reordering

// Allows customization of the target row for a particular row as it is being moved/reordered

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath
*)proposedDestinationIndexPath;

// Indentation

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath;// return 'depth'
of row for hierarchies

// Copy/Paste. All three methods must be implemented by the delegate.

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(5_0);

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullableid)sender
NS_AVAILABLE_IOS(5_0);

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullableid)sender
NS_AVAILABLE_IOS(5_0);

@end

UIKIT_EXTERN NSString *const UITableViewSelectionDidChangeNotification;

//**************************************************************

//UITableView继承于UIScrollView

NS_CLASS_AVAILABLE_IOS(2_0)@interface UITableView :
UIScrollView <NSCoding>

//UITableView创建,创建的时候必须指明style(默认UITableViewStylePlain)

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)styleNS_DESIGNATED_INITIALIZER;

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoderNS_DESIGNATED_INITIALIZER;

//UITableView风格(只读)

@property (nonatomic,readonly)
UITableViewStyle style;

//UITableView数据源

@property (nonatomic,weak,
nullable)id <UITableViewDataSource> dataSource;

//UITableView代理

@property (nonatomic,weak,
nullable)id <UITableViewDelegate> delegate;

//设置表视图cell的高度,统一的高度默认44px

@property (nonatomic)CGFloat rowHeight;

//设置UITableView种section的头部的高度

@property (nonatomic)CGFloat sectionHeaderHeight;

//设置UITableView种section的尾部的高度

@property (nonatomic)CGFloat sectionFooterHeight;

//设置一个行高的估计值(默认为0,表示没有估计,7.0之后可用)

注意:这个属性官方的解释是如果你的tableView的行高是可变的,那么设计一个估计高度可以加快代码的运行效率。

@property (nonatomic)CGFloat estimatedRowHeight
NS_AVAILABLE_IOS(7_0);

//下面这两个属性和上面相似,分别设置分区头视图和尾视图的估计高度(7.0之后可用)

@property (nonatomic)CGFloat estimatedSectionHeaderHeight
NS_AVAILABLE_IOS(7_0);

@property (nonatomic)CGFloat estimatedSectionFooterHeight
NS_AVAILABLE_IOS(7_0);

//设置分割线的位置

@property (nonatomic)UIEdgeInsets separatorInsetNS_AVAILABLE_IOS(7_0)UI_APPEARANCE_SELECTOR;

//设置UITableView的背景

@property (nonatomic,strong,
nullable)UIView *backgroundViewNS_AVAILABLE_IOS(3_2);

//刷新整个tableView

- (void)reloadData;

//重载索引栏

- (void)reloadSectionIndexTitles
NS_AVAILABLE_IOS(3_0);
// reloads the index bar.

@property (nonatomic,readonly)
NSInteger numberOfSections;

//每个分区section有多少行

- (NSInteger)numberOfRowsInSection:(NSInteger)section;

//获取分区的大小(包括头视图,所有行和尾视图)

- (CGRect)rectForSection:(NSInteger)section;

//根据分区分别获取头视图,尾视图和行的高度

- (CGRect)rectForHeaderInSection:(NSInteger)section;

- (CGRect)rectForFooterInSection:(NSInteger)section;

- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;

//获取某个点在tableView中的位置信息

- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;

//获取某个cell在tableView中的位置信息

- (nullable NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;

//根据一个矩形范围返回一个信息数组,数组中是每一行row的位置信息

- (nullable NSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect;

//通过位置路径获取cell

- (nullable __kindofUITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

//获取所有可见的cell

@property (nonatomic,readonly)
NSArray<__kindofUITableViewCell *> *visibleCells;

//获取所有可见行的位置信息

@property (nonatomic,readonly,
nullable)NSArray<NSIndexPath *> *indexPathsForVisibleRows;

//根据分区获取头视图

- (nullable UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);

//根据分区获取尾视图

- (nullable UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);

//使表视图定位到某一位置(行)

// 注意:indexPah参数是定位的位置,决定于分区和行号。animated参数决定是否有动画。scrollPosition参数决定定位的相对位置,它使一个枚举

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

//使表视图定位到选中行

- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

// Row insertion/deletion/reloading.

//开始块标志

- (void)beginUpdates;

//结束快标志

- (void)endUpdates;

//指定的indexSet所包含的一个或多个分区号对应的位置插入分区

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

//删除指定indexSet所包含的一个或多个分区号所对应的分区

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animationNS_AVAILABLE_IOS(3_0);

//将指定分区移动到另一个位置

- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSectionNS_AVAILABLE_IOS(5_0);

//插入一些行,animation参数是一个枚举

- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

//删除一些行,animation参数是一个枚举

- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

//重载一些行,animation参数是一个枚举

- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animationNS_AVAILABLE_IOS(3_0);

//移动某行

- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPathNS_AVAILABLE_IOS(5_0);

/*

当我们调用的上面的函数时,tableView会立刻调用代理方法进行刷新,如果其中我们所做的操作是删除某行,而然数据源数组我们可能并没有刷新,程序就会崩溃掉,原因是代理返回的信息和我们删除后不符

*/

//设置是否是编辑状态(编辑状态下的cell左边会出现一个减号,点击右边会划出删除按钮)

@property (nonatomic,getter=isEditing)
BOOL editing;

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

//设置cell是否可以被选中(默认为YES)

@property (nonatomic)BOOL allowsSelection
NS_AVAILABLE_IOS(3_0);

//设置cell编辑模式下是否可以被选中

@property (nonatomic)BOOL allowsSelectionDuringEditing;

//设置是否支持多选

@property (nonatomic)BOOL allowsMultipleSelection
NS_AVAILABLE_IOS(5_0);

//设置cell编辑模式下是否支持多选

@property (nonatomic)BOOL allowsMultipleSelectionDuringEditing
NS_AVAILABLE_IOS(5_0);

//获取选中cell的位置信息

@property (nonatomic,readonly,
nullable)NSIndexPath *indexPathForSelectedRow;

//获取多选cell的位置信息

@property (nonatomic,readonly,
nullable)NSArray<NSIndexPath *> *indexPathsForSelectedRowsNS_AVAILABLE_IOS(5_0);

//代码手动选中与取消选中某行

- (void)selectRowAtIndexPath:(nullableNSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;

- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

//tableView附件的相关方法

//设置索引栏最小显示行数

@property (nonatomic)NSInteger sectionIndexMinimumDisplayRowCount;

//设置索引栏字体颜色

@property (nonatomic,strong,
nullable)
UIColor *sectionIndexColorNS_AVAILABLE_IOS(6_0)UI_APPEARANCE_SELECTOR;

//设置索引栏背景颜色

@property (nonatomic,strong,
nullable)UIColor *sectionIndexBackgroundColor
NS_AVAILABLE_IOS(7_0)
UI_APPEARANCE_SELECTOR;

//设置索引栏被选中时的颜色

@property (nonatomic,strong,
nullable)UIColor *sectionIndexTrackingBackgroundColor
NS_AVAILABLE_IOS(6_0)
UI_APPEARANCE_SELECTOR;

//设置分割线的风格,这个风格是一个枚举

@property (nonatomic)UITableViewCellSeparatorStyle separatorStyle;

//设置分割线颜色

@property (nonatomic,strong,
nullable)UIColor *separatorColorUI_APPEARANCE_SELECTOR;

//设置分割线毛玻璃效果(IOS8之后可用)

@property (nonatomic,copy,
nullable)
UIVisualEffect *separatorEffectNS_AVAILABLE_IOS(8_0)UI_APPEARANCE_SELECTOR;

@property (nonatomic)BOOL cellLayoutMarginsFollowReadableWidth
NS_AVAILABLE_IOS(9_0);

//设置tableView头视图

@property (nonatomic,strong,
nullable)UIView *tableHeaderView;

//设置tableView尾视图

@property (nonatomic,strong,
nullable)UIView *tableFooterView;

//从复用池中取cell

- (nullable __kindofUITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;

//获取一个已注册的cell

- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(6_0);

//从复用池获取头视图或尾视图

- (nullable __kindofUITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(6_0);

//通过xib文件注册cell

- (void)registerNib:(nullableUINib *)nib forCellReuseIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(5_0);

//通过OC类注册cell

- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(6_0);

//通过xib文件和OC类获取注册头视图和尾视图

- (void)registerNib:(nullableUINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(6_0);

- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(6_0);

@end

//**************************************************************

//这个协议描绘数据模型

@protocol UITableViewDataSource<NSObject>

//必须实现的方法

@required

//每个组(section)有多少行(row)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

//每行的显示,通常通过设置reuseIdentifier进行cell的重复利用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

//选择实现的方法

@optional

//有多少组(section),默认1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

//设置每组的头标题

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

//设置每组的尾标题

- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

//该方法返回值决定指定indexPath对应的cell是否可以编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

//该方法返回值决定指定indexPath对应的cell是否可以移动

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

//右边一竖列索引显示的内容(eg:‘a’-‘z’)

- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;

//把每一组和右边的索引关联起来(eg:‘a’对应第一组),告诉tableview哪个section符合右边的某个索引标题

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index; //
tell table which section corresponds to section title/index (e.g. "B",1))

//当用户对指定表格行编辑(包括插入和删除)时触发该方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath
*)indexPath;

//该方法触发移动 通常对数据进行处理

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath
*)destinationIndexPath;

@end

//**************************************************************

//NSIndexPath类别,提供更方便的办法,用NSIndexPath去描绘section和row

@interface NSIndexPath (UITableView)

+ (instancetype)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;

@property (nonatomic,readonly)
NSInteger section;

@property (nonatomic,readonly)
NSInteger row;

@end

NS_ASSUME_NONNULL_END
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: