初始化方法
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)styleNS_DESIGNATED_INITIALIZER;
- (nullableinstancetype)initWithCoder:(NSCoder *)aDecoderNS_DESIGNATED_INITIALIZER;
属性
表视图的样式
@property (nonatomic,readonly) UITableViewStyle style;
@property (nonatomic,weak, nullable)id <UITableViewDataSource> dataSource;
@property (nonatomic,weak, nullable)id <UITableViewDelegate> delegate;
行高
@property (nonatomic)CGFloat rowHeight;
组的头部高度
@property (nonatomic)CGFloat sectionHeaderHeight;
组的脚部高度
@property (nonatomic)CGFloat sectionFooterHeight;
预测行高
@property (nonatomic)CGFloat estimatedRowHeight NS_AVAILABLE_IOS(7_0);// default is 0, which means there is no estimate
预测组的头高
@property (nonatomic)CGFloat estimatedSectionHeaderHeightNS_AVAILABLE_IOS(7_0);// default is 0, which means there is no estimate
预测组的脚高
@property (nonatomic)CGFloat estimatedSectionFooterHeightNS_AVAILABLE_IOS(7_0);// default is 0, which means there is no estimate
分隔线插入
@property (nonatomic)UIEdgeInsets separatorInset NS_AVAILABLE_IOS(7_0)UI_APPEARANCE_SELECTOR; // allows customization of the frame of cell separators
背景颜色
@property (nonatomic,strong, nullable)UIView *backgroundView NS_AVAILABLE_IOS(3_2);
刷新整个表视图
- (void)reloadData;
刷新索引栏
- (void)reloadSectionIndexTitlesNS_AVAILABLE_IOS(3_0);
组数
@property (nonatomic,readonly) NSInteger numberOfSections;
组数
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
某个组的位置和大小
- (CGRect)rectForSection:(NSInteger)section;
某个组的头标签的位置和大小
- (CGRect)rectForHeaderInSection:(NSInteger)section;
某个组的尾标签的位置和大小
- (CGRect)rectForFooterInSection:(NSInteger)section;
返回indexPath指定行的矩形
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
点击某一个点,判断是在哪一行上的信息
- (nullableNSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
获取单元格的信息
- (nullableNSIndexPath *)indexPathForCell:(UITableViewCell *)cell;
在某个区域里会返回多个单元格信息
- (nullableNSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect;
通过单元格路径得到单元格
- (nullable__kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
获取可见cells(界面上能显示出来cell)
@property (nonatomic,readonly) NSArray<__kindofUITableViewCell *> *visibleCells;
返回所有可见行的路径
@property (nonatomic,readonly, nullable)NSArray<NSIndexPath *> *indexPathsForVisibleRows;
设置头标签的视图
- (nullableUITableViewHeaderFooterView *)headerViewForSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);
设置脚标签的视图
- (nullableUITableViewHeaderFooterView *)footerViewForSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);
参数1控制滚动到哪一行,参数2控制滚动到该行的位置(顶端/中间/底部),参数3是否动画
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
例如:NSIndexPath *indexPath = [NSIndexPath indexPathForItem:8 inSection:0];
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
控制滚动到指定最近行的位置(顶端/中间/下方)
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
例如:[tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionBottom animated:YES];
// Row insertion/deletion/reloading.
只添加或删除才会更新行数
- (void)beginUpdates;
添加或删除后会调用添加或删除方法时才会更新
- (void)endUpdates;
插入一个或多个组,并使用动画
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
删除一个或多个组,并使用动画
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
更新一个或多个组,并使用动画
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animationNS_AVAILABLE_IOS(3_0);
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSectionNS_AVAILABLE_IOS(5_0);
在指定行插入行,并使用动画
- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
删除指定行
- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
整体刷新(每一行都会刷新)
- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animationNS_AVAILABLE_IOS(3_0);
移动个某一行到目标位置
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPathNS_AVAILABLE_IOS(5_0);
// Editing. When set, rows show insert/delete/reorder controls based on data source queries
是否允许编辑,默认是NO
@property (nonatomic,getter=isEditing) BOOL editing;
表格进入编辑状态,有动画
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
在非编辑下,行是否可以选中,默认为YES
@property (nonatomic)BOOL allowsSelection NS_AVAILABLE_IOS(3_0);
控制某一行时,是否可以编辑,默认为NO
@property (nonatomic)BOOL allowsSelectionDuringEditing;
是否可以选择多行,默认为NO
@property (nonatomic)BOOL allowsMultipleSelection NS_AVAILABLE_IOS(5_0);
在选择多行的情况下,是否可以编辑,默认为NO
@property (nonatomic)BOOL allowsMultipleSelectionDuringEditingNS_AVAILABLE_IOS(5_0);
// Selection
返回选定行的indexPath,单行
@property (nonatomic,readonly, nullable)NSIndexPath *indexPathForSelectedRow;
//返回选定行的indexPath数组,多行
@property (nonatomic,readonly, nullable)NSArray<NSIndexPath *> *indexPathsForSelectedRowsNS_AVAILABLE_IOS(5_0);
// Selects and deselects rows. These methods will not call the delegate methods (-tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath:), nor will it send out a notification.
根据选择需要滚动的行 是否动画 滚动位置
- (void)selectRowAtIndexPath:(nullableNSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
控制取消选中该表格中指定indexPath对应的表格行
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
// Appearance
显示某个组索引列表在右边当行数达到这个值,默认是0
@property (nonatomic)NSInteger sectionIndexMinimumDisplayRowCount;
选择某个部分的某行改变这一行上文本的颜色
@property (nonatomic,strong, nullable)UIColor *sectionIndexColor NS_AVAILABLE_IOS(6_0)UI_APPEARANCE_SELECTOR;
选中行不被触摸时的背景颜色
@property (nonatomic,strong, nullable)UIColor *sectionIndexBackgroundColorNS_AVAILABLE_IOS(7_0)UI_APPEARANCE_SELECTOR;
选中行被触摸时的背景颜色
@property (nonatomic,strong, nullable)UIColor *sectionIndexTrackingBackgroundColorNS_AVAILABLE_IOS(6_0)UI_APPEARANCE_SELECTOR;
设置单元格分隔线的样式
@property (nonatomic)UITableViewCellSeparatorStyle separatorStyle__TVOS_PROHIBITED;
分隔线的颜色 默认灰色
@property (nonatomic,strong, nullable)UIColor *separatorColor UI_APPEARANCE_SELECTOR__TVOS_PROHIBITED;
风格线效果 默认的可见
@property (nonatomic,copy, nullable)UIVisualEffect *separatorEffect NS_AVAILABLE_IOS(8_0)UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;// effect to apply to table separators
单元格布局边距是否按照可读宽度
@property (nonatomic)BOOL cellLayoutMarginsFollowReadableWidthNS_AVAILABLE_IOS(9_0);
设置组表的头标签视图
@property (nonatomic,strong, nullable)UIView *tableHeaderView;
设置组表的尾标签视图
@property (nonatomic,strong, nullable)UIView *tableFooterView;
获取重用队列里的单元格
- (nullable__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;
参数1:重用标识符 参数2:参数行indexPath
- (__kindofUITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(6_0);// newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
自定义分组头部的重用
- (nullable__kindof UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(6_0); // like dequeueReusableCellWithIdentifier:, but for headers/footers
// 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标识符
若使用nib,使用 registerNib: 注册,dequeue时会调用 cell 的 -(void)awakeFromNib
- (void)registerNib:(nullableUINib *)nib forCellReuseIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(5_0);
不使用nib,使用 registerClass: 注册, dequeue时会调用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(6_0);
向数据源注册 头标识符
- (void)registerNib:(nullableUINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(6_0);
- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
#ifndef SDK_HIDE_TIDE
// Focus
指定焦点是否应返回焦点离开时的最后一个焦点索引路径
@property (nonatomic)BOOL remembersLastFocusedIndexPathNS_AVAILABLE_IOS(9_0);// defaults to NO. If YES, when focusing on a table view the last focused index path is focused automatically. If the table view has never been focused, then the preferred focused index path is used.默认为没有。如果是,当聚焦一个表格视图,上一个索引路径自动聚焦。如果从来没有集中的表视图,那么首选集中使用索引路径。
#endif
@end
//_______________________________________________________________________________________________________________
// this protocol represents the data model object. as such, it supplies no information about appearance (including the cells)
@protocol UITableViewDataSource<NSObject>
@required
一个section有多少row 多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
通过单元格路径得到单元格 行中内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@optional
一个TableView有多少个section 多少组 默认1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented
头标签内容
- (nullableNSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // fixed font style. use custom view (UILabel) if you want something different固定的字体样式。如果你想要不同的东西,使用自定义视图(UILabel)
组头脚标签内容
- (nullableNSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
// Editing
该方法返回值决定指定indexPath对应的cell是否可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
// Moving/reordering
该方法返回值决定指定indexPath对应的cell是否可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
// Index
该方法返回值用于在表格右边建立一个浮动的索引
- (nullableNSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView__TVOS_PROHIBITED;
个性化定制索引跳转方式 点击索引,列表跳转到对应索引的行
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index__TVOS_PROHIBITED;
// Data manipulation - insert and delete support
当用户对指定表格行编辑(包括插入和删除)时触发该方法 提交编辑动作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
// Data manipulation - reorder / moving support
该方法触发移动 通常对数据进行处理 移动某一行到另一行
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
@end
//_______________________________________________________________________________________________________________
// This category provides convenience methods to make it easier to use an NSIndexPath to represent a section and row
@interface NSIndexPath (UITableView)
+ (instancetype)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;
组号
@property (nonatomic,readonly) NSInteger section;
行号
@property (nonatomic,readonly) NSInteger row;
@end
NS_ASSUME_NONNULL_END
//添加右侧分区索引值
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return self.letters;
}
//特殊指定点击分区索引值之后的跳转位置
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
// 默认情况是
return index;
}
//索引的文字颜色
_tableView.sectionIndexColor = [UIColor redColor];
//背景颜色
_tableView.sectionIndexBackgroundColor = [UIColor greenColor];
//按住以后的颜色
_tableView.sectionIndexTrackingBackgroundColor = [UIColor purpleColor];
//设置当cell总数超过多少的时候,才显示索引
_tableView.sectionIndexMinimumDisplayRowCount = 0;
//被点击后的被选中/高亮颜色
cell.selectionStyle = 1;
typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {
UITableViewCellSelectionStyleNone, //没有点击效果, 自定义也无效
UITableViewCellSelectionStyleBlue, //蓝色, 已失效
UITableViewCellSelectionStyleGray, //灰色, 默认值
UITableViewCellSelectionStyleDefault //灰色
};
//可以指定被选中是显示的视图,selectionStyle不能为UITableViewCellSelectionStyleNone
UIView *bgView = [UIView new];
bgView.backgroundColor=[UIColor greenColor];
cell.selectedBackgroundView = bgView;
隐藏多余Cell
//当表格行数不足一页时,会有多余的cell显示,去掉
_tableView.tableFooterView = [UIView new];