1.继承关系

UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳。UITableView的代理协议也继承了UIScrollView的代理协议,可以通过实现UIScrollView的代理方法,监听UITableView的变化。在UITableView中没有列的概念,只有行的概念,数据都是按行显示的。

 

2.常用属性

// 数据源和代理
@property (nonatomic, weak, nullable) id <UITableViewDataSource> dataSource;

@property (nonatomic, weak, nullable) id <UITableViewDelegate> delegate;

// tableview的视图风格(只读)
@property (nonatomic, readonly) UITableViewStyle style;

// 设置 行高、区头高度、区尾高度(当代理方法没有实现时才有效)
@property (nonatomic) CGFloat rowHeight;
@property (nonatomic) CGFloat sectionHeaderHeight;
@property (nonatomic) CGFloat sectionFooterHeight;

// 如果你的tableView的行高是可变的,那么设计一个估计高度可以加快代码的运行效率。
@property (nonatomic) CGFloat estimatedRowHeight 
@property (nonatomic) CGFloat estimatedSectionHeaderHeight 
@property (nonatomic) CGFloat estimatedSectionFooterHeight 

// 设置分割线的位置(通过这个属性,可以手动设置分割线的位置偏移)
@property (nonatomic) UIEdgeInsets separatorInset 


例如:tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);//分割线边界对齐

// 设置tableView背景view视图
@property (nonatomic, strong, nullable) UIView *backgroundView;

 

3.常用方法详解


 3.1 数据刷新

// 刷新数据
- (void)reloadData;

// 这个方法常用于新加或者删除了索引类别而无需刷新整个表视图的情况下。
- (void)reloadSectionIndexTitles;


 3.2 信息

// 分区数
- (NSInteger)numberOfSections;

// 根据分区来获取所在区的行数
- (NSInteger)numberOfRowsInSection:(NSInteger)section;

// 获取分区的大小(包括头视图,所有行和尾视图)
- (CGRect)rectForSection:(NSInteger)section;

// 根据分区分别获取头视图,尾视图和行的高度
- (CGRect)rectForHeaderInSection:(NSInteger)section;
- (CGRect)rectForFooterInSection:(NSInteger)section;
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;

// 获取某个点在tableView中的位置信息
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;

// 获取某个cell在tableView中的位置信息
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;

// 根据一个矩形范围返回一个信息数组,数组中是每一行row的位置信息
- (NSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect;

// 通过位置路径获取cell
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

// 获取所有可见的cell
- (NSArray *)visibleCells;

// 获取所有可见行的位置信息
- (NSArray *)indexPathsForVisibleRows;

// 根据分区获取头视图
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);

// 根据分区获取尾视图
- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);

// 使表示图定位到某一位置(行)
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

/* 注意:indexPah参数是定位的位置,决定于分区和行号。animated参数决定是否有动画。scrollPosition参数决定定位的相对位置,它使一个枚举,如下: typedef NS_ENUM(NSInteger, UITableViewScrollPosition) { UITableViewScrollPositionNone, // 同下 UITableViewScrollPositionTop, // 定位完成后,将定位的行显示在tableView的顶部 UITableViewScrollPositionMiddle, // 定位完成后,将定位的行显示在tableView的中间 UITableViewScrollPositionBottom // 定位完成后,将定位的行显示在tableView的底部 }; */ // 使表示图定位到选中行 - (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

 

 

3.3 /行的插入、删除、刷新

 

//    开始、结束更新
- (void)beginUpdates;

//    在这里写 插入、删除、刷新、移动操作。
例如:

[tab beginUpdates];
[tab deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
[dataArray removeObjectAtIndex:1];
[tab endUpdates];

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

 

 

 

 

3.4 编辑操作

 

// 设置是否是编辑状态(编辑状态下的cell左边会出现一个减号,点击右边会划出删除按钮)
@property (nonatomic, getter=isEditing) BOOL editing;//(默认为NO)
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

// 设置cell是否可以被选中(默认为YES)
@property (nonatomic) BOOL allowsSelection;

// 设置cell编辑模式下是否可以被选中
@property (nonatomic) BOOL allowsSelectionDuringEditing;

// 设置是否支持多选
@property (nonatomic) BOOL allowsMultipleSelection;

// 设置编辑模式下是否支持多选
@property (nonatomic) BOOL allowsMultipleSelectionDuringEditing 

 

3.5 选中cell的相关操作

// 获取选中cell的位置信息
- (NSIndexPath *)indexPathForSelectedRow;

// 获取多选cell的位置信息
- (NSArray *)indexPathsForSelectedRows NS_AVAILABLE_IOS(5_0);

// 代码手动选中与取消选中某行
- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

 


3.6 其他属性设置

// 设置索引栏最小显示行数
@property (nonatomic) NSInteger sectionIndexMinimumDisplayRowCount;

// 设置索引栏字体颜色
@property (nonatomic, strong, nullable) UIColor *sectionIndexColor;

// 设置索引栏背景颜色
@property (nonatomic, strong, nullable) UIColor *sectionIndexBackgroundColor;

// 设置索引栏被选中时的颜色
@property (nonatomic, strong, nullable) UIColor *sectionIndexTrackingBackgroundColor;

// 设置分割线的风格
@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle;

/*
typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
UITableViewCellSeparatorStyleNone,//无线
UITableViewCellSeparatorStyleSingleLine,//有线
UITableViewCellSeparatorStyleSingleLineEtched
};
*/

// 设置分割线颜色
@property (nonatomic, strong, nullable) UIColor *separatorColor;

// 设置分割线毛玻璃效果(IOS8之后可用)
@property (nonatomic, copy, nullable) UIVisualEffect *separatorEffect;
@property (nonatomic) BOOL cellLayoutMarginsFollowReadableWidth NS_AVAILABLE_IOS(9_0);

// 设置tableView头视图
@property (nonatomic, strong, nullable) UIView *tableHeaderView;

// 设置tableView尾部视图
@property (nonatomic, strong, nullable) UIView *tableFooterView;

// 从复用池中取cell
- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;

// 获取一个已注册的cell
- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath 

// 从复用池获取头视图或尾视图
- (UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier 

// 通过xib文件注册cell
- (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier;

// 通过OC类注册cell
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier 

// 通过xib文件和OC类获取注册头视图和尾视图
- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier 
- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier 

 

 


posted on 2019-07-23 16:27  夜之独行者  阅读(108)  评论(0编辑  收藏  举报