iOS-UIKit框架学习—UITableView

UITableView在iOS开发中是使用最广泛的一个控件,用于展示单个项目数据的列表,同时也可以进行选择和编辑操作。

@class UITableView;

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;
// 滑动时cell的风格
@property (nonatomic, readonly) UITableViewRowActionStyle style;
// 标题
@property (nonatomic, copy, nullable) NSString *title;
// 背景颜色
@property (nonatomic, copy, nullable) UIColor *backgroundColor;
// 毛玻璃效果
@property (nonatomic, copy, nullable) UIVisualEffect* backgroundEffect;

@end

// 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

//_______________________________________________________________________________________________________________
// this represents the display and behaviour of the cells.

@protocol UITableViewDelegate

@optional

// 即将显示tableviewcell时调用
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
// 即将显示header时调用,在cell之后调用
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
// 即将显示footer时调用,在header之后调用
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
// 在删除cell之后调用,停止显示cell的时候调用,界面不显示cell时。
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
// 停止显示header的时候调用
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
// 停止显示footer的时候调用
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);

// 设置某行cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
// 设置header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
// 设置footer高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

// 设置每行cell的估算高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);
// 对可变的header 高度的估算
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
// 对可变的footer 高度的估算
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);

// 设置第section分组的headers自定义视图
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
// 设置第section分组的footer自定义视图
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

// 可手动调用,在其他的触发事件中 传入tableview 以及indexPath参数,进行操作
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;

// Selection
// 设置是否允许cell高亮状态
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
// 按下cell未弹起时调用,即即将高亮时调用,如果return no 则点击的时候没有反应,但是还是会执行此方法
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
// 按下cell弹起时调用
- (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);
// 选择指定的cell时调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
// 取消选择指定cell时调用
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);

// 编辑
// 返回每一行cell的编辑模式, 可以再次设置add或者删除操作
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
// cell左滑删除时,删除按钮的标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
// 自定义编辑左滑后出现的界面
- (nullable NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

// 编辑模式下是否缩进指定行背景
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;

// 进入编辑模式时调用
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath __TVOS_PROHIBITED;
// 编辑结束时调用
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(nullable NSIndexPath *)indexPath __TVOS_PROHIBITED;

// 移动
// 在编辑状态下,返回可以进行移动重新排序的indexpath
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;

// 缩进
// 设置cell的缩进
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath;

// 复制/粘贴
// 是否在指定行显示菜单,返回值为YES时,长按显示菜单
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0);
// 弹出菜单后回调用此方法。
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender NS_AVAILABLE_IOS(5_0);
// 选择菜单完成后,调用此方法。
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender NS_AVAILABLE_IOS(5_0);

// cell是否可以获得焦点
- (BOOL)tableView:(UITableView *)tableView canFocusRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
// 是否更新聚焦范围内发生的内容
- (BOOL)tableView:(UITableView *)tableView shouldUpdateFocusInContext:(UITableViewFocusUpdateContext *)context NS_AVAILABLE_IOS(9_0);
// 聚焦范围内容更新完成后调用
- (void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator NS_AVAILABLE_IOS(9_0);
// 设置聚焦时首选视图
- (nullable NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView NS_AVAILABLE_IOS(9_0);

@end


NS_CLASS_AVAILABLE_IOS(2_0) @interface UITableView : UIScrollView 
// cell的构造方法,自定义cell时,如果要初始化设置cell属性时,可以重写该方法,在方法内部设置
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
// UITableView的类型: plain类型和group分组类型
@property (nonatomic, readonly) UITableViewStyle style;
// 数据源
@property (nonatomic, weak, nullable) id  dataSource;
// 代理
@property (nonatomic, weak, nullable) id  delegate;
// 预读取数据
@property (nonatomic, weak) id prefetchDataSource NS_AVAILABLE_IOS(10_0);
// 行高
@property (nonatomic) CGFloat rowHeight;
// 分组Header的高度
@property (nonatomic) CGFloat sectionHeaderHeight;
// 分组Footer的高度
@property (nonatomic) CGFloat sectionFooterHeight;   // will return the default
// 估计行高
@property (nonatomic) CGFloat estimatedRowHeight NS_AVAILABLE_IOS(7_0); //
// 分组Header估计高度
@property (nonatomic) CGFloat estimatedSectionHeaderHeight NS_AVAILABLE_IOS(7_0);
// 分组Footer估计高度
@property (nonatomic) CGFloat estimatedSectionFooterHeight NS_AVAILABLE_IOS(7_0);
// 分割线内边距
@property (nonatomic) UIEdgeInsets separatorInset NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
// 背景view
@property (nonatomic, strong, nullable) UIView *backgroundView NS_AVAILABLE_IOS(3_2);
// 重新载入tableview所有cell  一般是在数据源有改变的时候
- (void)reloadData;
// 重新载入,section的索引标题。
- (void)reloadSectionIndexTitles NS_AVAILABLE_IOS(3_0);
// 获取TableView共有多少组
@property (nonatomic, readonly) NSInteger numberOfSections;
// 获取第section组共有多少行
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
// 获取某一组的frame,头部frame,尾部frame,cell的frame
- (CGRect)rectForSection:(NSInteger)section;
- (CGRect)rectForHeaderInSection:(NSInteger)section;
- (CGRect)rectForFooterInSection:(NSInteger)section;
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
// 返回指定坐标点的indexPath
- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
// 返回指定cell所在的indexPath
- (nullable NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;
// 返回指定范围内的所有indexPath的数组
- (nullable NSArray *)indexPathsForRowsInRect:(CGRect)rect;
// 返回indexPath所指向的cell
- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
// 当前可以看到的cell数组
@property (nonatomic, readonly) NSArray<__kindof UITableViewCell *> *visibleCells;
// 返回tableView中标识的可见的indexPath数组
@property (nonatomic, readonly, nullable) NSArray *indexPathsForVisibleRows;
// 设置tableView的headerView
- (nullable UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
indexPathsForVisibleRows;
// 设置tableView的footerView
- (nullable UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
// 根据传入的indexPath,滚动到相对应的位置,第二个参数是控制对应的cell再滚动后处于tableview的顶部/底部/中部等
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
// 滚动到被选中项。滚动后处于tableview的顶部/底部/中部等
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

// 开始一系列的增删改移操作
- (void)beginUpdates;
// 操作结束更新数据
- (void)endUpdates;
// 对section进行插入,删除,刷新,移动操作
- (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);
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection NS_AVAILABLE_IOS(5_0);

// 对行进行插入,删除,刷新,移动批量操作
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);

// 编辑状态 默认为NO
@property (nonatomic, getter=isEditing) BOOL editing;
// 设置编辑状态动画显示
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
// cell 是否可以选择 默认为YES
@property (nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0);
// 编辑模式是否可以选择 默认为NO
@property (nonatomic) BOOL allowsSelectionDuringEditing;
// 是否可以多选 默认为NO
@property (nonatomic) BOOL allowsMultipleSelection NS_AVAILABLE_IOS(5_0);
// 编辑模式是否可以多选 默认为NO
@property (nonatomic) BOOL allowsMultipleSelectionDuringEditing NS_AVAILABLE_IOS(5_0);

// 获取选中行的indexPath
@property (nonatomic, readonly, nullable) NSIndexPath *indexPathForSelectedRow;
// 返回一个多行选中的indexpath数组
@property (nonatomic, readonly, nullable) NSArray *indexPathsForSelectedRows NS_AVAILABLE_IOS(5_0);

// 调用此方法,此indexpath的cell被选中,若此cell不再可视范围内,自动按照最后一个参数的方式进行滚动
- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
// 取消此indexpath的选中状态
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;


// 索引条设置
@property (nonatomic) NSInteger sectionIndexMinimumDisplayRowCount;
// 索引条字体颜色
@property (nonatomic, strong, nullable) UIColor *sectionIndexColor NS_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 __TVOS_PROHIBITED;
// 分割线的颜色
@property (nonatomic, strong, nullable) UIColor *separatorColor UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
// 分割线的玻璃效果
@property (nonatomic, copy, nullable) UIVisualEffect *separatorEffect NS_AVAILABLE_IOS(8_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
// 判断是否需要根据内容留有空白
@property (nonatomic) BOOL cellLayoutMarginsFollowReadableWidth NS_AVAILABLE_IOS(9_0);

// tableView的头部视图,不要和section header混淆
@property (nonatomic, strong, nullable) UIView *tableHeaderView;
// tableView的底部视图,不要和section footer混淆
@property (nonatomic, strong, nullable) UIView *tableFooterView;
// 从缓冲池获取cell
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;
// 从缓冲池获取指定下标的cell
- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
// 从缓冲池获取header,footer
- (nullable __kindof UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);

// 注册xib创建的cell
- (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
// 注册cell
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
// 注册xib创建的header,footer
- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
// 注册header,footer
- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
// 是否自动将焦点上次集中索引的路径的单元格上 默认值为NO
@property (nonatomic) BOOL remembersLastFocusedIndexPath NS_AVAILABLE_IOS(9_0); .

@end

@protocol UITableViewDataSource

@required

// 每组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// 返回显示的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

@optional
// tablaView上有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
// 返回某个section对应的header标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
// 返回某个section对应的footer标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
// 设置cell为可编辑模式
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
// 设置cell为可移动模式
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
// 设置右边栏的索引目录数组 e.g. "ABCD...Z#"
- (nullable NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView __TVOS_PROHIBITED;
// 点击右侧索引表项时调用 e.g. "B",1
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index __TVOS_PROHIBITED;
// 提交编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
// 移动时使用
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

@end

@protocol UITableViewDataSourcePrefetching 

@required

// 设置预读取的优先排序数组
- (void)tableView:(UITableView *)tableView prefetchRowsAtIndexPaths:(NSArray *)indexPaths;

@optional

// 预读取indexPath数组
- (void)tableView:(UITableView *)tableView cancelPrefetchingForRowsAtIndexPaths:(NSArray *)indexPaths;

@end


@interface NSIndexPath (UITableView)

// 获取indexPath
+ (instancetype)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;
// 获取第几组
@property (nonatomic, readonly) NSInteger section;
// 获取第几行
@property (nonatomic, readonly) NSInteger row;

@end
// UITableView风格样式
typedef NS_ENUM(NSInteger, UITableViewStyle) {
    UITableViewStylePlain,          // 平铺式(补充:有section时,自动悬浮在顶部)
    UITableViewStyleGrouped         // 分段式
};

// 选中某一行滚动到tableView顶、中、下的位置
typedef NS_ENUM(NSInteger, UITableViewScrollPosition) {
    UITableViewScrollPositionNone,
    UITableViewScrollPositionTop, // 滚动到顶部
    UITableViewScrollPositionMiddle, // 滚动到中部
    UITableViewScrollPositionBottom // 滚到到底部
};

// 删除/添加时,cell过渡动画效果
typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
    UITableViewRowAnimationFade,       // 褪去
    UITableViewRowAnimationRight,      // 从右边滑入(或滑出)
    UITableViewRowAnimationLeft,       // 从左边滑入(或滑出)
    UITableViewRowAnimationTop,        // 从顶部插入(或删除)
    UITableViewRowAnimationBottom,     // 从底部插入(或删除)
    UITableViewRowAnimationNone,       // 无效果
    UITableViewRowAnimationMiddle,     //
    UITableViewRowAnimationAutomatic = 100
};

// 在编辑状态下左划cell时,cell右侧显示的按钮类型
typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) {
    UITableViewRowActionStyleDefault = 0, // 红色
    UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault,
    UITableViewRowActionStyleNormal
} NS_ENUM_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

// 设置索引的标题为搜索.
UIKIT_EXTERN NSString *const UITableViewIndexSearch NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
// 选择发生改变后发送通知的key
UIKIT_EXTERN NSNotificationName const UITableViewSelectionDidChangeNotification;


你可能感兴趣的:(iOS-UIKit框架学习—UITableView)