iOS——UITableView

来源:互联网 发布:淘宝天猫内部券公众号 编辑:程序博客网 时间:2024/05/16 05:07

一、UITableView 介绍

1. 基本概念

①UITableView 是一种列表控件,用户可以选中、删除、排序列表中的每个条目
②UITableView 对象只能显示一列数据,但是没有行的限制
③一般,创建 UITableView 对象要通过某个 视图控制器 来创建和释放

2. 数据源

①UITableView 对象要通过 数据源 才能正常工作,UITableView 对象会向其指向的数据源对象 查询要显示的行数、显示的数据等,因此,没有数据源的 UITableView 对象只是一个 “空壳”,什么都做不了
②只要是遵循了 UITableViewDataSource 协议的 OC 对象,都可以作为 UITableView 的数据源(dataSource 属性指向的对象)

3. 委托

①通常,要为 UITableView 对象设置委托对象,以便能在该 UITableView 对象发生特定的事件时做出相应的处理
②只要是遵循了 UITableViewDelegate 协议的 OC 对象,都可以作为 UITableView 的委托(delegate 属性指向的对象)

4. UITableViewController

①UITableViewController 对象满足上述的所有条件,即可以创建释放 UITableView 对象、可以作为 UITableView 对象的数据源、作为 UITableView 对象的委托对象
②UITableViewController 是 UIViewController 的子类,所以它也有 view 属性;它的 view 属性指向一个 UITableView 对象
③UITableViewController 对象在创建后,会自动创建一个 UITableView 对象并将 view 指向此 UITableView 对象,接着把 dataSource 属性和 delegate 属性指向这个 UITableViewController 对象

二、UITableView 属性解释

1. 保存 UITableView 对象的风格

@property (nonatomic,readonly)UITableViewStyle style;

[objc] view plain copy
  1. //UITableViewStyle  
  2. typedef NS_ENUM(NSInteger, UITableViewStyle) {  
  3.     UITableViewStylePlain,          // 普通样式  
  4.     UITableViewStyleGrouped         // 分组样式  
  5. };  
普通样式                                                  分组样式



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;

[objc] view plain copy
  1. // UIEdgeInsets  
  2. typedef struct UIEdgeInsets {  
  3.       
  4. CGFloat top, left, bottom, right;  
  5.       
  6. } UIEdgeInsets;  
系统默认的 UITableView 对象左边的分割线并没有顶到左边的边界,通过这个属性,可以手动设置分割线的偏移
例如 : 设置分割线距离左边和右边都有一定的距离

[objc] view plain copy
  1. self.tableView.separatorInset = UIEdgeInsetsMake(030030);  
[objc] view plain copy
  1. // 创建 UIEdgeInsets 对象的方法  
  2.     UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) {  
  3.           
  4.         UIEdgeInsets insets = {top, left, bottom, right};  
  5.         return insets;  
  6.       
  7.     }  

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;


16. 获取指定分组的表尾视图的大小

- (CGRect)rectForFooterInSection:(NSInteger)section;



17. 获取指定分组、指定行的 表格行的大小

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


18. 获取某一个点在 UITableView 中的位置;如果指定位置在所有的表格行之外返回 nil

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

19. 获取某一个 cell 在 UITableView 中的位置;如果 cell 没有显示则返回 nil

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


20. 根据指定的矩形范围,获取信息数组,数组中的每个元素都是每一行的位置信息

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


21. 根据指定的分组、指定的表格行,获取对应的 cell 对象

- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; 


22. 获取所有的可见的 cell 对象,以数组的形式返回

@property (nonatomicreadonly)NSArray<__kindof UITableViewCell *> *visibleCells;


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

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


24. 根据指定分组获取表头视图

- (nullable UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section;


25. 根据指定分组获取表尾视图

- (nullable UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section;


26. 使 UITableView 对象定位到某一行

indexPath : 定位到指定的分组和行

animated : 是否有动画效果

scrollPosition : 定位的相对位置

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

[objc] view plain copy
  1. // UITableViewScrollPosition  
  2.     typedef NS_ENUM(NSInteger, UITableViewScrollPosition) {  
  3.         UITableViewScrollPositionNone,   // 和 UITableViewScrollPositionTop 一样  
  4.         UITableViewScrollPositionTop,    // 定位完成后,将定位的行显示到 UITableView 对象的顶部  
  5.         UITableViewScrollPositionMiddle, // 定位完成后,将定位的行显示到 UITableView 对象的中间  
  6.         UITableViewScrollPositionBottom  // 定位完成后,将定位的行显示到 UITableView 对象的底部    
  7.     };  

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;

[objc] view plain copy
  1. // UITableViewRowAnimation  
  2.     typedef NS_ENUM(NSInteger,UITableViewRowAnimation) {  
  3.         UITableViewRowAnimationFade,            // 淡入淡出  
  4.         UITableViewRowAnimationRight,           // 从右滑入  
  5.         UITableViewRowAnimationLeft,            // 从左滑入  
  6.         UITableViewRowAnimationTop,             // 从上滑入  
  7.         UITableViewRowAnimationBottom,          // 从下滑入  
  8.         UITableViewRowAnimationNone,            // 没有动画  
  9.         UITableViewRowAnimationMiddle,  
  10.         UITableViewRowAnimationAutomatic =100  // 自动选择合适的动画  
  11.     };  
 

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 显示方法

1. 设置索引栏最小显示行数

@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; 

[objc] view plain copy
  1. // UITableViewCellSeparatorStyle  
  2. typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {  
  3.     UITableViewCellSeparatorStyleNone,              // 无线  
  4.     UITableViewCellSeparatorStyleSingleLine,        // 有线  
  5.     UITableViewCellSeparatorStyleSingleLineEtched   // 仅支持 UITableViewStyleGrouped 风格  
  6. };  

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 方法

1. 通过 xib 文件注册一个 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 其他方法

1. 是否记住最后一此操作的 位置信息;默认为 NO;在 iOS 9 之后才可以用

@property (nonatomic)BOOL remembersLastFocusedIndexPath;


九、UITableViewDataSource

1. 必须实现的方法

①返回每个分组的表格行的个数

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


②返回在指定的 indexPath 位置处的 cell 对象,用于填充每个表格行

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


2. 可选实现的方法

①返回 UITableView 对象的分组个数;默认为 1

- (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;


⑥返回索引栏的标题数组(实现此方法,会在 UITableView 对象的右侧显示每个分组的索引)

- (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;

[objc] view plain copy
  1. // UITableViewCellEditingStyle  
  2.     typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {  
  3.         UITableViewCellEditingStyleNone,    // 无编辑操作  
  4.         UITableViewCellEditingStyleDelete,  // 删除操作  
  5.         UITableViewCellEditingStyleInsert   // 插入操作  
  6.     };  


⑨UITableView 对象的 表格行(cell 对象)被移动时调用

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



 十、UITableViewDelegate

1. 表格行(cell 对象)将要显示时调用

- (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;


8. 设置指定分组的表头视图的高

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


9. 设置指定分组的表尾视图的高

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


10. 设置指定表格行的行高的估计值(对于高度可变的情况下,提高效率)

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


11. 设置指定分组的表头视图的高的估计值(对于高度可变的情况下,提高效率)

- (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;


22. 设置 UITableView 对象被编辑时的状态风格;默认为 UITableViewCelEditingStyleDelete 删除风格

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


25. 设置编辑时背景是否缩进;默认为 YES;只适用于 UITableViewStyleGrouped 风格

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


26. 将要编辑某一行 表格行 时调用

- (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;               


29. 返回缩进水平

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath;

0 0
原创粉丝点击