iOS组件之UITableView详解

引言

UITableView简单来说是一个表视图,继承于UIScrollView(有关UIScrollView请参照《iOS组件之UIScrollView详解》),用于显示和编辑信息的分层列表。

目录

1、tableView的基本属性及方法
2、访问tableView的画图区
3、访问cells和sections
4、滚动tableView
5、插入、删除、移动行和sections
6、管理和编辑cell
7、管理sections
8、配置
9、iOS5.0新增
10、iOS6.0新增
11、iOS7.0
12、UITableViewDataSource协议
    12.1必须实现
    12.2可选
        12.2.1编辑模式
    12.2.2数据操作  
13、UITableViewDelegate协议(可选)
    13.1定制化
    13.2iOS6新增
    13.3可变高度支持
    13.4iOS7.0新增
    13.5header 和 footer Section 信息
    13.6附属公开的接口
    13.7选择
    13.8编辑
    13.9移动
    13.10缩格
    13.11复制/粘贴

1、tableView的基本属性及方法

// 初始化

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style;

// 设置tableView的样式(只读)

UITableViewStyle style;

// 设置数据资源代理

id <UITableViewDataSource> dataSource;

// 设置代理

id <UITableViewDelegate> delegate;

// 设置每个row的高度,这个高度将被当成默认高度

CGFloat rowHeight;

// 设置sectionHeader高度,这个高度将被当成默认高度

CGFloat sectionHeaderHeight;   

// 设置sectionFooter高度,这个高度将被当成默认高度

CGFloat sectionFooterHeight;   

// 设置tableView的背景view

UIView *backgroundView;

// 刷新tableView

- (void)reloadData;

// 刷新tableView中的section

- (void)reloadSectionIndexTitles;

// sections的数量

- (NSInteger)numberOfSections;

// 下标为section中的row数量

- (NSInteger)numberOfRowsInSection:(NSInteger)section;

2、访问tableView的画图区

// 返回指定section的矩形

- (CGRect)rectForSection:(NSInteger)section; 

// 返回indexPath指定行的矩形

- (CGRect)rectForHeaderInSection:(NSInteger)section;

// 返回section的footer矩形

- (CGRect)rectForFooterInSection:(NSInteger)section;

// 返回section的header矩形

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

3、访问cells和sections

// 通过cell得到这个cell在tableView的位置

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

// 根据一个几何点返回indexPath,如果超过边界返回nil

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

// 根据一个几何的矩形返回矩形所覆盖的行,返回是一个indexPath数组

- (NSArray *)indexPathsForRowsInRect:(CGRect)rect;         

// 根据IndexPath返回cell

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

//

- (NSArray *)visibleCells;
- (NSArray *)indexPathsForVisibleRows;

4、滚动tableView

// 滚动到指定位置

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

// 同上

- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

5、插入、删除、移动行和sections

// 和endUpdates一起用,让插入、删除、选择操作同时动画,没用过

- (void)beginUpdates;

//

- (void)endUpdates;

//根据indexPath数组插入行

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

//根据indexPath数组删除行

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

//移动一行到另一行

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

//插入sections

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

//删除sections

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

//移动section

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

6、管理和编辑cell

// YES进入编辑模式,tableview cell会出现插入、删除、重排序的控件

BOOL editing;

// 设置进入退出编辑模式

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

7、管理sections

//是否允许用户选取一行

BOOL allowsSelection;

//返回选定行的indexPath,单行

- (NSIndexPath *)indexPathForSelectedRow;

// 根据indexPath选择一行

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

// 根据indexPath取消选择这一行

- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

8、配置

// 指定当tableView中多少行的时候开始显示IndexList,默认的设置是NSIntegerMax,即默认是不显示indexList的

NSInteger sectionIndexMinimumDisplayRowCount;

// 分隔线的样式(默认为UITableViewCellSeparatorStyleSingleLine)

UITableViewCellSeparatorStyle separatorStyle;

// cell之间的分割线

UIColor *separatorColor;              

// 设置headerView

UIView *tableHeaderView;                            

// 设置footerView

UIView *tableFooterView;                            

// 必须要实现的方法,与tableView同生同死

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;  

9、iOS5.0新增

// 移动section

- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;

// 移动一行到另一行

- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;

// 返回选定行的indexPath数组,多行

- (NSArray *)indexPathsForSelectedRows;

//

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier;

// 编辑模式时是否可选取一行

BOOL allowsSelectionDuringEditing;

// 是否选取多行,缺省为NO(iOS 5.0)

BOOL allowsMultipleSelection;

// 编辑模式时可否选取多行

BOOL allowsMultipleSelectionDuringEditing;

10、iOS6.0新增

// 我想在numberOfRowsInSection方法内获取该section的header View。须要在使用之前用 tableView 的 registerNib:forHeaderFooterViewReuseIdentifier: 或 registerClass:forHeaderFooterViewReuseIdentifier:。

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

// 我想在numberOfRowsInSection方法内获取该section的footer View。须要在使用之前用 tableView 的 registerNib:forHeaderFooterViewReuseIdentifier: 或 registerClass:forHeaderFooterViewReuseIdentifier:。

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

// 设置索引号颜色

UIColor *sectionIndexColor;

// 设置索引号背景颜色

UIColor *sectionIndexTrackingBackgroundColor;

// 注册(没有测试有什么用)

- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier; 
- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier;
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
- (id)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier;

11、iOS7.0

// 预设SectionFooter的高度

CGFloat estimatedSectionFooterHeight;
UIEdgeInsets separatorInset;
UIColor *sectionIndexBackgroundColor;

// 预设row的高度

CGFloat estimatedRowHeight;     

// 预设SectionHeader的高度

CGFloat estimatedSectionHeaderHeight; 

12、UITableViewDataSource协议

12.1必须实现

// 返回每个section中row的数量

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

// 返回每个UITableViewCell

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

12.2可选

// 返回section的个数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;              

// section headerView的title文本内容

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; 

// section footerView的title文本内容

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
12.2.1 编辑模式

// 设置可编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
12.2.1.1 移动

// 设置是否可移动

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

// 设置索引号

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

// 点击索引触发

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
12.2.2 数据操作

// 插入/删除指定数据

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

// 重新排序/移动数据操作

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

13、UITableViewDelegate协议(可选)

13.1定制化

// cell将要显示的时候被调用

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

13.2iOS6新增

// HeaderView将要显示的时候被调用

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section;

// FooterView将要显示的时候被调用

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section;

// cell结束显示时被调用

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath;

// HeaderView结束显示时被调用

- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section;

// FooterView结束显示时被调用

- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section;

13.3可变高度支持

// 根据indexPath返回row的高度

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

// 根据indexPath返回HeaderView的高度

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

// 根据indexPath返回FooterView的高度

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

13.4iOS7.0新增

// 代理函数或者设置estimatedRowHeight属性值,提供一个预估高度值,对于可变高度的cell的表格视图可改进性能

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

// 代理函数或者设置estimatedRowHeight属性值,提供一个预估高度值,对于可变高度的Header的表格视图可改进性能

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

// 代理函数或者设置estimatedRowHeight属性值,提供一个预估高度值,对于可变高度的Footer的表格视图可改进性能

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

13.5 header 和 footer Section 信息

// 根据section创建header section

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

// 根据section创建footer section

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

13.6 附属公开的接口

// 回调设置辅助按钮被点击后的事件

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;

13.7 选择

// 回调设置某行是否当被点击后处于高亮状态

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);

// 回调当某行处于高亮状态时的行为

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);

// 回调当某行失去高亮状态时的行为

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);

// 回调某行将要被选中的行为

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;

// 回调某行将要被取消选中的行为

- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath;

// 回调某行已经被选中点击的行为

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

// 回调某行已经取消选中的行为

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;

13.8 编辑

// 回调设置某行进入了哪种编辑模式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

// 回调设置某行进入删除模式的删除按钮名字

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath;

// 是否允许对row进行编辑

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

// 开始编辑row时触发

- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;

// 结束编辑row时触发

- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath;

13.9 移动

// 移动Row时会把触发,在交换Row位置的时候,必须同时交换DataSource中数据的位置。

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;               

13.10 缩格

// 设置内容缩进

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

13.11 复制/粘贴

// 回调设置某行被长按是否出现菜单栏

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

// 回调设置菜单栏是否显示哪些菜单栏选项

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender;

// 回调点击菜单栏选项触发的事件

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender;





Comments