iOS——UITableView
来源:互联网 发布:淘宝天猫内部券公众号 编辑:程序博客网 时间:2024/05/16 05:07
一、UITableView 介绍
1. 基本概念
2. 数据源
3. 委托
4. UITableViewController
二、UITableView 属性解释
1. 保存 UITableView 对象的风格
@property (nonatomic,readonly)UITableViewStyle style;
普通样式 分组样式
2. 保存 UITableView 对象的数据源对象
@property (nonatomic,weak,nullable)id <UITableViewDataSource> dataSource;
3. 保存 UITableView 对象的委托对象
@property (nonatomic,weak,nullable)id <UITableViewDelegate> delegate;
4. 设置 UITableView 对象的表格行高度;默认为 44
@property (nonatomic)CGFloat rowHeight;
5. 设置 UITableView 对象的段表头视图的高度
@property (nonatomic)CGFloat sectionHeaderHeight;
6. 设置 UITableView 对象的段表尾视图的高度
@property (nonatomic)CGFloat sectionFooterHeight;
7. 设置一个表格行高的估计值;默认为 0,表示没估计;在 iOS 7 之后才可以用
@property (nonatomic)CGFloat estimatedRowHeight;
这个属性的解释是 : 当表格行的高度是变化的,那么设计一个估计高度就可以加快代码的运行率
8. 设置一个段表头视图高的估计值;默认为 0;在 iOS 7 之后才可以用
@property (nonatomic)CGFloat estimatedSectionHeaderHeight
9. 设置一个段表尾视图高的估计值;默认为 0;在 iOS 7 之后才可以用
@property (nonatomic)CGFloat estimatedSectionFooterHeight
10. 设置 UITableView 对象的分割线的位置
@property (nonatomic)UIEdgeInsets separatorInset;
系统默认的 UITableView 对象左边的分割线并没有顶到左边的边界,通过这个属性,可以手动设置分割线的偏移例如 : 设置分割线距离左边和右边都有一定的距离
11. 设置 UITableView 对象的背景视图
@property (nonatomic,strong,nullable)UIView * backgroundView
12. 保存 UITableView 对象的分组数
@property (nonatomic,readonly)NSInteger numberOfSections;
13. 保存 UITableView 对象在指定分组中的行数
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
14. 获取指定分组的大小(包括表头视图,表尾视图以及所有的行)
- (CGRect)rectForSection:(NSInteger)section;
15. 获取指定分组的表头视图的大小
- (CGRect)rectForHeaderInSection:(NSInteger)section;
- (CGRect)rectForFooterInSection:(NSInteger)section;
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
19. 获取某一个 cell 在 UITableView 中的位置;如果 cell 没有显示则返回 nil
- (nullable NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;
- (nullable NSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect;
- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
22. 获取所有的可见的 cell 对象,以数组的形式返回
@property (nonatomic, readonly)NSArray<__kindof UITableViewCell *> *visibleCells;
23. 获取所有可见行的位置信息
@property (nonatomic,readonly,nullable)NSArray<NSIndexPath *> *indexPathsForVisibleRows;
24. 根据指定分组获取表头视图
- (nullable UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section;
- (nullable UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section;
26. 使 UITableView 对象定位到某一行
indexPath : 定位到指定的分组和行
animated : 是否有动画效果
scrollPosition : 定位的相对位置
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
27. 使 UITableView 对象定位到选中的行
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPositionanimated:(BOOL)animated;
三、UITableView 方法
1. 刷新 UITableView 对象,即重新显示表格行
- (void)reloadData;
2. 刷新索引栏;常用于新增或删除了某个索引为无需刷新整个UITableView 对象
- (void)reloadSectionIndexTitles;
3. 插入分组
- (void)insertSections:(NSIndexSet*)sections withRowAnimation:(UITableViewRowAnimation)animation;
4. 删除分组
- (void)deleteSections:(NSIndexSet*)sections withRowAnimation:(UITableViewRowAnimation)animation;
5. 重载分组
- (void)reloadSections:(NSIndexSet*)sections withRowAnimation:(UITableViewRowAnimation)animation;
6. 移动分组
- (void)moveSection:(NSInteger)sectiontoSection:(NSInteger)newSection;
7. 插入一些行在指定的 indexPath 处
- (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;
10. 移动一些指定的行
- (void)moveRowAtIndexPath:(NSIndexPath*)indexPath toIndexPath:(NSIndexPath*)newIndexPath;
11. 开始块标记
- (void)beginUpdates;
12. 结束块标记
- (void)endUpdates;
当我们调用上述的插入、删除、重载和移动方法时,UITableView对象会立刻调用代理方法来进行刷新;例如:我们执行的是删除某一行,然而数据源并没有及时更新,此时应用就会崩溃,因为代理返回的信息和删除后的不一样;而我们可以将要做的操作全都写在开始标记块和结束标记块中,此时,只有当程序运行到结束标记块时,才会调用代理方法刷新
四、UITableView编辑模式
1. 判断 UITableView 对象是否处于编辑模式的(编辑模式下 cell 对象左边会有一个红色的减号,点击会在右边出现 delete 按钮)
@property(nonatomic,getter=isEditing) BOOL editing;
2. 设置UITableView 对象是否处于编辑模式
- (void)setEditing:(BOOL)editinganimated:(BOOL)animated;
3. 设置 表格行(cell 对象)是否可以被选中;默认为 YES
@property (nonatomic)BOOL allowsSelection;
4. 设置 表格(cell 对象)在编辑模式下是否可以被选中;默认是 NO
@property (nonatomic)BOOL allowsSelectionDuringEditing;
5. 设置 表格行(cell 对象)是否可以多选;默认是 NO
@property (nonatomic)BOOL allowsMultipleSelection;
6. 设置 表格行(cell 对象)在编辑模式下是否可以多选;默认为 NO
@property (nonatomic)BOOL allowsMultipleSelectionDuringEditing;
五、UITableView 的 cell 对象选中方法
1. 获取选中的 cell 对象的位置
@property (nonatomic,readonly,nullable)NSIndexPath *indexPathForSelectedRow;
2. 获取多个选中 cell 对象的位置信息
@property (nonatomic,readonly,nullable)NSArray<NSIndexPath *> *indexPathsForSelectedRows;
3. 手动选择某个表格行,不会调用代理中的方法
- (void)selectRowAtIndexPath:(nullableNSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
4. 手动删除某个表格行,不会调用代理中的方法
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
六、UITableView 显示方法
@property (nonatomic)NSInteger sectionIndexMinimumDisplayRowCount;
2. 设置索引栏字体颜色
@property (nonatomic,strong,nullable)UIColor *sectionIndexColor;
3. 设置索引栏背景颜色
@property (nonatomic,strong,nullable)UIColor *sectionIndexBackgroundColor;
4. 设置索引栏被选中时的颜色
@property (nonatomic,strong,nullable)UIColor *sectionIndexTrackingBackgroundColor;
5.设置分割线的风格;默认是 UITableViewCellSeparatorStyleSingleLine
@property (nonatomic)UITableViewCellSeparatorStyle separatorStyle;
6. 设置分割线的颜色;默认是标准灰
@property (nonatomic,strong,nullable)UIColor *separatorColor
7. 设置分割线的毛玻璃效果;在 iOS 8 之后才可以用
@property (nonatomic,copy,nullable)UIVisualEffect *separatorEffect;
8. 设置 UITableView 对象的表头视图;默认是 nil
@property (nonatomic,strong,nullable)UIView *tableHeaderView;
9. 设置 UITableView 对象的表尾视图;默认是 nil
@property (nonatomic,strong,nullable)UIView *tableFooterView;
10. 根据指定的 标识符 从复用池中取出 cell
- (nullable__kindofUITableViewCell *)dequeueReusableCellWithIdentifier:(NSString*)identifier;
11.
- (__kindofUITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
12. 从复用池中取出 表头视图或表尾视图
- (nullable__kindofUITableViewHeaderFooterView*)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier
七、UITableView 注册 cell 方法
- (void)registerNib:(nullableUINib *)nib forCellReuseIdentifier:(NSString *)identifier
2. 通过类注册一个 cell 对象
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString*)identifier;
3. 通过 xib 文件注册 表头视图或表尾视图
- (void)registerNib:(nullableUINib *)nib forHeaderFooterViewReuseIdentifier:(NSString*)identifier;
4. 通过类注册 表头视图或表尾视图
- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier;
八、UITableView 其他方法
@property (nonatomic)BOOL remembersLastFocusedIndexPath;
九、UITableViewDataSource
1. 必须实现的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
②返回在指定的 indexPath 位置处的 cell 对象,用于填充每个表格行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
2. 可选实现的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
②返回指定 section 分组的表头视图的标题
- (nullableNSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
③返回指定 section 分组的表尾视图的标题
- (nullableNSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
④返回指定的 indexPath 表格行是否可以被编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath;
⑤返回指定的 indexPath 表格行是否可以被移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath;
- (nullableNSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView*)tableView
⑦设置索引栏标题对应的分区
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index
⑧UITableView 对象处于编辑模式下调用的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
十、UITableViewDelegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
2. 表头视图将要显示时调用
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
3. 表尾视图将要显示时调用
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
4. 表格行(cell 对象)显示完成时调用
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath
5. 表头视图已经显示时调用
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section
6. 表尾视图已经显示时调用
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section
7. 设置指定表格行的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
9. 设置指定分组的表尾视图的高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath*)indexPath;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section;
12. 设置指定分组的表尾视图的高的估计值(对于高度可变的情况下,提高效率)
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section;
13. 自动义表头视图
- (nullableUIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
14. 自定义表尾视图
- (nullableUIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
15. 设置选中的表格行(cell 对象)是否高亮
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath*)indexPath;
16. 表格行 高亮时调用的方法
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath*)indexPath;
17. 表格行 取消高亮时调用的方法
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath*)indexPath;
18. 即将选中某个表格行(cell 对象)时调用的方法;返回一个新的 NSIndexPath 对象
- (nullableNSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
19. 即将取消选中某个表格行(cell 对象)时调用的方法;返回一个新的 NSIndexPath 对象
- (nullableNSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
20. 已经选中某个表格行(cell 对象)时调用的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;
21. 已经取消选中某个表格行(cell 对象)时调用的方法
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath;
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
23. 自定义删除按钮的标题
- (nullableNSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath;
24. 自定义 UITableView 对象被编辑时右边的按钮,按钮类型为 UITableViewRowAction
- (nullableNSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath;
27. 结束编辑某一行 表格行 时调用
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(nullableNSIndexPath *)indexPath;
28. 移动特定的某一表格行
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath;
- iOS——UITableView
- iOS——UITableView
- iOS开发学习笔记——UITableview
- IOS系列——UITableview晋级篇
- IOS学习笔记——UITableView(一)
- IOS学习笔记——UITableView (二)
- IOS学习笔记——UITableView(三)
- iOS —— UITableView 常用API
- UITableView的基础知识——IOS
- iOS控件——UITableView详解
- iOS UITableView简介 —— HERO博客
- iOS——UITableView的优化
- iOS学习之——UITableView详解
- ios 自定义折叠控件 ——UITableView
- iOS—UITableView Cell重用机制
- 黑马程序员—IOS控件UITableView详解
- iOS—简单封装UITableView使用1
- iOS—简单封装UITableView使用2
- javacef
- M1卡破解(自从学校升级系统之后,还准备在研究下)
- 常见问题及要注意的知识点
- 我的算法8
- GPS信号结构
- iOS——UITableView
- java访问权限
- 前端js怎么实现文件下载
- L1-013. 计算阶乘和
- java中几中常见的排序方式汇总及二分法查找
- oracle忘记密码与解锁用户
- poj 2255 Tree Recovery (二叉树的顺序遍历)
- 恢复eclipse 视图格式
- 2017年华为实习生招聘笔试题1