UITableViewController
是UIViewController的子类,UITableViewController默认扮演了3种角色:视图控制器、UITableView的数据源和代理UITableViewController的view是个UITablView,由UITableViewController负责设置和显示这个对象。UITableViewController对象被创建后,会将这个UITableView对象的dataSource和delegate指向UITableViewController自己。
一、UITableView常用方法
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
// 初始化一个UITableView,并且设置表格样式
- (void)reloadData
// 重新访问数据源,刷新界面
- (NSInteger)numberOfSections
// 分区的个数
- (NSInteger)numberOfRowsInSection:(NSInteger)section
// 第section分区的行数
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
// 通过indexPath找到对应的UITableViewCell对象
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
// 是否要开启编辑模式
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
// 取消选中某一行,让被选中行的高亮颜色消失(带动画效果)
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
// 通过identifier在(缓存)池中找到对应的UITableViewCell对象
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
// 移除indexPaths范围内的所有行
@property(nonatomic,readonly) UITableViewStyle style
//表格样式
@property(nonatomic,assign) id <UITableViewDataSource> dataSource
// 数据源
@property(nonatomic,assign) id <UITableViewDelegate> delegate
// 代理
@property(nonatomic,getter=isEditing) BOOL editing
// 是否为编辑模式
@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle
// 设置分隔线的样式
@property(nonatomic,retain) UIColor *separatorColor
// 设置分隔线的颜色
@property(nonatomic,retain) UIView *tableHeaderView
// 表头显示的视图
@property(nonatomic,retain) UIView *tableFooterView
// 表尾显示的视图
@property(nonatomic) BOOL allowsSelection
// 是否允许选中行
@property(nonatomic) BOOL allowsSelectionDuringEditing
// 是否允许在编辑模式下选中行
@property(nonatomic) BOOL allowsMultipleSelection
// 是否允许选中多行
@property(nonatomic) BOOL allowsMultipleSelectionDuringEditing
// 是否允许在编辑模式下选中多行
二、UITableView
1.数据展示的条件
(1) UITableView的所有数据都是由数据源(dataSource)提供的,所以要想在UITableView展示数据,必须设置UITableView的dataSource数据源对象
(2)要想当UITableView的dataSource对象,必须遵守UITableViewDataSource协议,实现相应的数据源方法
(3)当UITableView想要展示数据的时候,就会给数据源发送消息(调用数据源方法),UITableView会根据方法返回值决定展示怎样的数据
2.数据展示的过程
(1)先调用数据源的
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
得知一共有多少组
(2)然后调用数据源的
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
得知第section组一共有多少行
(3)然后调用数据源的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
得知第indexPath.section组 第indexPath.row 行显示怎样的cell(显示什么内容)
3.常见数据源方法
(1) 一共有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
(2)第section组一共有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
(3)第indexPath.section组 第indexPath.row行显示怎样的cell(显示什么内容)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
(4)第section组显示怎样的头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
(5) 第section组显示怎样的尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
4.tableView刷新数据的方式
(1) 修改模型数据
(2) 刷新表格
* reloadData --- 整体刷新(每一行都会刷新)
* - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation ---局部刷新
5.性能优化
(1) 定义一个循环利用标识
static NSString *ID = @"C1";
(2) 从缓存池中取出可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
(3) 如果缓存池中没有可循环利用的cell
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
(4) 覆盖cell上面的数据
cell.textLabel.text = [NSString stringWithFormat:@"第%d行数据", indexPath.row];