【iOS学习】UITableView.h阅读笔记 #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 <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; // 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<UITableViewRowAction *> *)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 <UITableViewDataSource> dataSource; @property (nonatomic, weak, nullable) id <UITableViewDelegate> 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<NSIndexPath *> *)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<NSIndexPath *> *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; /**标注: *上述15,16两个方法可以实现列表翻了很多页数据后,点击按钮返回到第一页或者顶部. */ 三、行的插入/删除/刷新 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<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 8.删除某些行 - (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 9.刷新某些分区的行 - (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)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<NSIndexPath *> *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<NSString *> *)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; |
|
来自: 九辰夕 > 《UITableView列表》