1.UITableView概述
我们生活中用到的应用几乎都用到了UITableView,例如QQ,微信,网易新闻,新浪微博等。它继承UIScrollView,可以在垂直方向上滚动,它里面的UITableViewCell可以自定义添加控件,功能强大。
2.常用属性和方法
UITableView它有两种样式:UITableViewStylePlain,UITableViewStyleGrouped 当tableView的样式为 plain的时候,如果设置了组头和组尾文本,在滚动的时候就会有一个悬浮效果(组头,组尾)
初始化: UITableView = [UITableView alloc] initWithFrame:(CGRect) style:(UITableViewStyle)];
1>数据源方法:<UITableViewDataSource>
@required 必须实现的方法
//返回每组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//返回UITableVIewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@optional
//返回组数,默认是一组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
//返回组头标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
//返回组尾标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
// Editing
tableView开启编辑模式
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
1. numberOfSectionInTableView:
2. numberOfRowsInSection:
3. heightForRowAtIndexPath: (数据源中有多少条数据就调用多少次, 为了计算tableView的滚动范围)
4. cellForRowAtIndexPath:
再次调用 heightForAtIndexPath: 确认高度
@optional
//返回每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
//返回组头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
//返回组尾的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
//返回自定义的组头
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
//返回自定仪的组尾
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
//选中某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
//不选中某一行
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
3>常用属性及方法:
//行高
@property (nonatomic) CGFloat rowHeight;
//组头高度
@property (nonatomic) CGFloat sectionHeaderHeight;
//组尾高度
@property (nonatomic) CGFloat sectionFooterHeight;
//分割线的缩进
@property (nonatomic) UIEdgeInsets separatorInset
//是否允许选中,默认是yes
@property (nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0); // default is YES.
//在编辑的时候是否允许选中,默认是no
@property (nonatomic) BOOL allowsSelectionDuringEditing; // default is NO.
//是否允许多个选中,默认是no
@property (nonatomic) BOOL allowsMultipleSelection NS_AVAILABLE_IOS(5_0); // default is NO.
//在编辑的时候是否允许多个选中,默认是no
@property (nonatomic) BOOL allowsMultipleSelectionDuringEditing NS_AVAILABLE_IOS(5_0); //
//分割线的样式,默认是UITableViewCellSeparatorStyleSingleLine
@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle __TVOS_PROHIBITED; // default is UITableViewCellSeparatorStyleSingleLine
//分割线的颜色
@property (nonatomic, strong, nullable) UIColor *separatorColor UI_A
//tableView的组头
@property (nonatomic, strong, nullable) UIView *tableHeaderView;
// default is nil.
//tableView的组尾
@property (nonatomic, strong, nullable) UIView *tableFooterView;
//根据重用标识符到缓存池中找相应的cell
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;
//根据重用标识符到缓存池中找相应的HeaderFooterView
- (nullable __kindof UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
// Beginning in iOS 6, clients can register a nib or class for each cell.
// If all reuse identifiers are registered, use the newer -dequeueReusableCellWithIdentifier:forIndexPath: to guarantee that a cell instance is returned.
// Instances returned from the new dequeue method will also be properly sized when they are returned.
//为cell注册一个xib/class,绑定一个重用标识符
- (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
//为HeaderFooterView注册一个xib/class,绑定一个重用标识符
- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
//插入某一行
- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
//删除某一行
- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
//刷新某一行(单行刷新)
- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
//全部刷新
- (void)reloadData
//滚动到某一行
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;