相信对于学习iOS的童鞋,平时接触最多的,用到最多的就是UITableView了,那么你对UITableView了解多少呢?你真的会正确使用它吗?
下面就让我们一起走进UITableView的世界,去一点点的解析它,读懂它。
懒加载创建UITableView
-(UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStyleGrouped];
_tableView.separatorStyle = UITableViewCellStyleDefault;
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor whiteColor];
}
return _tableView;
}
separatorStyle
cell显示的样式
UITableViewCellStyleDefault, // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边)
UITableViewCellStyleValue1, // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边)
UITableViewCellStyleValue2, // 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边)
UITableViewCellStyleSubtitle // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)
style
UITableView的显示风格
UITableViewStylePlain, //普通样式
UITableViewStyleGrouped // 分组样式
UITableView的内部属性
设置表示图的行高(默认为44)
@property (nonatomic) CGFloat rowHeight
设置分组样式时的头视图高度和尾视图高度(当代理方法没有实现时才有效)
@property (nonatomic) CGFloat sectionHeaderHeight
@property (nonatomic) CGFloat sectionFooterHeight
设置一个行高的估计值(默认为0,表示没有估计,7.0之后可用),注意:这个属性官方的解释是如果你的tableView的行高是可变的,那么设计一个估计高度可以加快代码的运行效率。
下面这两个属性和上面相似,分别设置分区头视图和尾视图的估计高度(7.0之后可用)
@property (nonatomic) CGFloat estimatedRowHeight
@property (nonatomic) CGFloat estimatedSectionHeaderHeight
@property (nonatomic) CGFloat estimatedSectionFooterHeight
设置分割线的位置
@property (nonatomic) UIEdgeInsets separatorInset
设置分割线的风格
@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
设置tableView头视图
@property (nonatomic, strong, nullable) UIView *tableHeaderView
设置tableView尾视图
@property (nonatomic, strong, nullable) UIView *tableFooterView
从复用池中取cell
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier
获取一个已注册的cell
- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
从复用池获取头视图或尾视图
- (nullable __kindof UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier
通过xib文件注册cell
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier;
通过OC类注册cell
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier
上面两个方法是IOS6之后的方法。
通过xib文件和OC类获取注册头视图和尾视图
- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier
- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier
更改自定义分隔符值的解释方式。默认值是UITableViewSeparatorInsetFromCellEdges
@property (nonatomic) UITableViewSeparatorInsetReference separatorInsetReference
枚举类型
typedef NS_ENUM(NSInteger, UITableViewSeparatorInsetReference) {
UITableViewSeparatorInsetFromCellEdges,//默认值,表示separatorInset是从cell的边缘的偏移量
UITableViewSeparatorInsetFromAutomaticInsets//表示separatorInset属性值是从一个insets的偏移量
}
设置tableView背景view视图
@property (nonatomic, strong, nullable) UIView *backgroundView
获取section个数(只读属性)
@property (nonatomic, readonly) NSInteger numberOfSections;
设置每个section的行数
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
获取分组的大小(包括头视图,所有行和尾视图)
- (CGRect)rectForSection:(NSInteger)section;
根据分组分别获取头视图,尾视图和行的高度
- (CGRect)rectForHeaderInSection:(NSInteger)section;
- (CGRect)rectForFooterInSection:(NSInteger)section;
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
获取某个点在tableView中的位置信息
- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
获取某个cell在tableView中的位置信息
- (nullable NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;
根据一个矩形范围返回一个信息数组,数组中是每一行row的位置信息
- (nullable NSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect;
通过位置路径获取cell
- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
获取所有可见的cell
@property (nonatomic, readonly) NSArray<__kindof UITableViewCell *> *visibleCells;
获取所有可见行的位置信息
@property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *indexPathsForVisibleRows;
根据分组获取头、尾视图
- (nullable UITableViewHeaderFooterView *)headerViewFor