UIScrollView,UICollectionView 和UITableView的属性和方法
UIScrollView,UICollectionView 和UITableView
三者之间的关系:UIScrollView是 UICollectionView 和 UITableView 的父类.
UIScrollView
一个可以滚动的视图控件,可以用来查看比屏幕大很多的视图内容.
常用属性:
contentOffset:当前显示的区域相对于frame顶点的区域
contentSize:屏幕能滚动到的最大区域(画布)
contentInset:scrollview的contentview的顶点相对于scrollview的位置
directionalLockEnabled:是否禁止在某一方向的滚动
bounces:是否有弹框效果
alwaysBounceVertical:是否在垂直方向有反弹效果
alwaysBounceHorizontal:是否在水平方向上有反弹效果
pagingEnabled:是否有分页效果
scrollEnabled:是否能滚动
showsHorizontalScrollIndicator:是否显示水平滚动条
showsVerticalScrollIndicator:是否显示垂直滚动条
scrollIndicatorInsets:滚动指示器从封闭滚动视图中被嵌入的距离
indicatorStyle:滚动控制器的风格
decelerationRate:用户离开屏幕后滚动的减速速率
indexDisplayMode://数据显示方式
tracking:用户是否触摸内容并初始化滚动
dragging:是否开始滚动
decelerating:手指在离开屏幕后,屏幕是否继续滚动
delaysContentTouches:滚动视图是否延迟处理触摸下压事件
canCancelContentTouches:触摸内容视图是否总是跟踪
minimumZoomScale:最小缩放比例
maximumZoomScale:最大缩放比例
zoomScale:缩放比例
bouncesZoom:规定滚动视图是否在缩放超出最大值和最小值动画时播放内容缩放动画
zooming:是否缩放
zoomBouncing:缩放超出缩放限制
scrollsToTop:是否滚动到顶部
panGestureRecognizer:当前用于滑动手势的手势识别器
pinchGestureRecognizer:当前用于扩张/收缩手势的手势识别器
directionalPressGestureRecognizer:按压手势移动方向
keyboardDismissMode:键盘消失方式
refreshControl:更新控制
常用方法
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;//设定当前显示的区域相对于frame顶点的区域
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;//滚动内容的指定区域以便使内容在屏幕中可见
- (void)flashScrollIndicators; //短暂地显示滚动指示器
- (BOOL)touchesShouldBegin:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event inContentView:(UIView *)view;//(方法被滚动视图调用)以调整滚动视图处理滚动手势的方式
- (BOOL)touchesShouldCancelInContentView:(UIView *)view;//触摸是否可以取消
- (void)setZoomScale:(CGFloat)scale animated:(BOOL)animated ;//设置缩放比例
- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated;//将内容视图缩放到指定的Rect中
UICollectionView
常用属性:
collectionViewLayout:布局
prefetchingEnabled:是否预加载
backgroundView:背景视图
allowsSelection:是否允许 item可以点击
allowsMultipleSelection:是否允许多选
indexPathsForSelectedItems:选中的 item
numberOfSections:显示多少 item
visibleCells:界面上显示的 cell
indexPathsForVisibleItems:当前可视界面上显示的 item 的 indexPath
remembersLastFocusedIndexPath:是否记住最后操作的 indexPath
常用方法:
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout;//用layout来初始化布局
- (instancetype)initWithCoder:(NSCoder *)aDecoder://通过 xib 文件来初始化
- (void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;//通过类来注册
- (void)registerNib:(nullable UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;//通过 xib 来注册
- (void)registerClass:(nullable Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;//用 class来注册一个带补充视图的类
- (void)registerNib:(nullable UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;//通过 xib 来注册一个带补充视图的类
- (UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;//通过identifier来复用 cell
- (UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;//通过 Identifier 获取复用 ReusableView
- (NSArray<NSIndexPath *> *)indexPathsForSelectedItems;//选中的 items 的 indexPath
- (void)selectItemAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition;//指定选中的 item 的动画和位置
- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;//取消选中的 item
- (void)reloadData; //刷新数据
- (void)setCollectionViewLayout:(UICollectionViewLayout *)layout animated:(BOOL)animated; //是否动态设置布局
- (void)setCollectionViewLayout:(UICollectionViewLayout *)layout animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion ;//是否动态设置布局并回调
- (UICollectionViewTransitionLayout *)startInteractiveTransitionToCollectionViewLayout:(UICollectionViewLayout *)layout completion:(nullable UICollectionViewLayoutInteractiveTransitionCompletion)completion;//切换 layout
- (void)finishInteractiveTransition;//layout 切换完成
- (void)cancelInteractiveTransition;//取消切换 layout
- (NSInteger)numberOfSections;//组
- (NSInteger)numberOfItemsInSection:(NSInteger)section;//每组有多少 items
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;//返回 item 的布局属性
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;//返回重用视图的布局属性
- (NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point;//点击的 item 的 indexpath
- (NSIndexPath *)indexPathForCell:(UICollectionViewCell *)cell;//cell 的 indexPath
- (UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath;//indexPath 对应的 cell
- (NSArray<__kindof UICollectionViewCell *> *)visibleCells;//当前可见视图 celsl 的数组
- (NSArray<NSIndexPath *> *)indexPathsForVisibleItems;//当前可见视图的 items 的 indexPath 的数组
- (UICollectionReusableView *)supplementaryViewForElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;//indexPath 对应的重用视图
- (NSArray<UICollectionReusableView *> *)visibleSupplementaryViewsOfKind:(NSString *)elementKind;//当前可见的重用视图数组
- (NSArray<NSIndexPath *> *)indexPathsForVisibleSupplementaryElementsOfKind:(NSString *)elementKind;//当前可见辅助视图的 indexPath 的数组
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated//滚动到 indexPath 的 item;
- (void)insertSections:(NSIndexSet *)sections;//插入组
- (void)deleteSections:(NSIndexSet *)sections;//删除组
- (void)reloadSections:(NSIndexSet *)sections;//刷新某一组
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;//移动组
- (void)insertItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;//插入 items
- (void)deleteItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;//删除 items
- (void)reloadItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;//刷新 items
- (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;//移动 item
- (void)performBatchUpdates:(void (^ __nullable)(void))updates completion:(void (^ __nullable)(BOOL finished))completion;//一次性完成插入/删除等操作
- (BOOL)beginInteractiveMovementForItemAtIndexPath:(NSIndexPath *)indexPath ;//是否允许排序
- (void)updateInteractiveMovementTargetPosition:(CGPoint)targetPosition;//更新 item 的位置
- (void)endInteractiveMovement;//移动完成后
- (void)cancelInteractiveMovement;//取消更新(恢复到原始的位置)
UITableView
常用属性:
style:tableview 的样式
rowHeight:cell行高
sectionHeaderHeight:组头行高
sectionFooterHeight:组尾行高
estimatedRowHeight:预定义行高
estimatedSectionHeaderHeight:预定义组头高
estimatedSectionFooterHeight:预定义组尾高
separatorInset:分割线的偏移量
backgroundView:背景视图
numberOfSections:组数
visibleCells:当前可见的 cell
indexPathsForVisibleRows:可见 cell 的 indexPath
editing:编辑
allowsSelection:非编译状态下是否允许选中
allowsSelectionDuringEditing//编译状态下允许某行可以选中
allowsMultipleSelection:是否允许多选
allowsMultipleSelectionDuringEditing:在编译状态下是否允许多选
indexPathForSelectedRow:选中的 cell 的 indexPath
indexPathsForSelectedRows:选中的多个的 cell 的 indexPath
sectionIndexMinimumDisplayRowCount;显示 rowcount 的时候的最小 sectionIndex
sectionIndexColor:改变某一行的颜色
sectionIndexBackgroundColor:改变某一行的背景色
sectionIndexTrackingBackgroundColor:选中某部分的背景色
separatorColor:选中 cell 分割线的颜色
separatorEffect:毛玻璃效果
tableHeaderView:头视图
tableFooterView:尾视图
remembersLastFocusedIndexPath:记录最后一个焦点的 indexPath
常用方法:
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style;带 frame 的初始化方法
- (instancetype)initWithCoder:(NSCoder *)aDecoder;//通过 IB 初始化
- (void)reloadData;//刷新数据
- (void)reloadSectionIndexTitles;//刷新索引栏
- (NSInteger)numberOfRowsInSection:(NSInteger)section;//每组有多少行
- (CGRect)rectForSection:(NSInteger)section; //获取某个组的位置和大小
- (CGRect)rectForHeaderInSection:(NSInteger)section;//获取某个组的组头的位置和大小
- (CGRect)rectForFooterInSection:(NSInteger)section;//获取某个组的组尾的位置和大小
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;//获取某一行的位置和大小
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point; //由点确定在该行的 indexPath
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;//获取 cell 的 indexPath
- (NSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect; //获取多行的 indexPath 数组
- (__kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; //设置 cell
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section;//设置组头
- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section;//设置组尾
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;//将某行滚动到特点位置
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;//将选中的行滚动到特定位置
- (void)beginUpdates;//和下面方法成对使用,表示开始动态改变某一行
- (void)endUpdates;//和上面的方法成对使用,表示动态改变某行结束
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;//动态插入一行
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;//动态删除一组
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;//刷新一组
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;//移动某一组
- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;//插入多行
- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;//删除多行
- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;//刷新多行
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;//移动某一行
- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;//选中行滚动到特定位置
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;//取消选中
- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;//设置 cell 的重用标识符
- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;//设置某些 cell 的重用标识符
- (UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier;//设置头尾视图的重用标识符
- (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier;//通过 xib注册带重用标识符的 cell
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier;//通过类注册带重用标识符的 cell
- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier;通过 xib 注册重用的cell头尾视图
- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier;通过类注册重用的 cell 的头尾视图
附件: