码迷,mamicode.com
首页 > 其他好文 > 详细

UITableView

时间:2015-01-14 22:47:18      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

UITableView
 
UITableView 是iOS中显示数据列表最常用的一个控件,支持垂直滚动
 
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;
 

UITableViewDataSource 常用方
 
@required
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// 第section分区一共有多少行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
// 创建第section分区第row行的UITableViewCell对象(indexPath包含了section和row)
@optional
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;    // 一共有多少个分区
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
// 第section分区的头部标题

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
// 第section分区的底部标题
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
// 某一行是否可以编辑(删除)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
// 某一行是否可以移动来进行重新排序
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
// UITableView右边的索引栏的内容

UITableViewDelegate 常用方法
 
l- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
// 选中了UITableView的某一行
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
// 某一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
// 第section分区头部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
// 第section分区尾部的高度

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
// 第section分区头部显示的视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
// 第section分区尾部显示的视图
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
// 设置每一行的等级缩进(数字越小,等级越高)

UITableViewCell
 
UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行
UITableViewCell是UIView的子类,内部有个默认的子视图:contentView。contentView是UITableViewCell所显示内容的父视图,并负责显示一些辅助指示视图。辅助指示视图的作用是显示一个表示动作的图标,可以通过设置UITableViewCell的accessoryType来显示,默认是UITableViewCellAccessoryNone(不显示辅助指示视图)
 
UITableViewCell的contentView
 
contentView下默认有3个子视图,其中的2个是UILabel(通过UITableViewCell的textLabel和detailTextLabel属性访问),第3个是UIImageView(通过UITableViewCell的imageView属性访问)
UITableViewCell还有一个UITableViewCellStyle属性,用于决定使用contentView的哪些子视图,以及这些子视图在contentView中的位置
 

UITableViewCell对象的重用原理
 
iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存。要解决该问题,需要重用UITableViewCell对象
重用原理:当滚动列表时,部分 UITableViewCell 会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当 UITableView 要求 dataSource 返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的 UITableViewCell,dataSource 会用新的数据配置这个 UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象

还有一个非常重要的问题:有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell(如短信聊天布局),所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的UITableViewCell
解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象
 
重用UITableViewCell对象
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"Text %i", indexPath.row];
    return cell;
}

UITableViewCell的常用属性

backgroundView     //设置背景
selectedBackgroundView     //设置被选中时的背景视图
selectionStyle属性  //可设置UITableViewCell被选中时的背景颜色:
UITableViewCellSelectionStyleNone  //没有颜色
UITableViewCellSelectionStyleBlue  //蓝色(io6默认)
UITableViewCellSelectionStyleGray  //灰色(io7默认)
 
UITableView的编辑模式
 
UITableView有个editing属性,设置为YES时,可以进入编辑模式。在编辑模式下,可以管理表格中的行,比如改变行的排列顺序、增加行、删除行,但不能修改行的内容
多种方式开启编辑模式

@property(nonatomic,getter=isEditing) BOOL editing

- (
void)setEditing:(BOOL)editing animated:(BOOL)animated
 
删除UITableView的行
首先要开启编辑模式
实现UITableViewDataSource的如下方法:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 如果UITableView提交的是删除指令
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 删除真实数据
        // [self.data removeObjectAtIndex:indexPath.row];
        // 删除UITableView中的某一行(带动画效果)
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        // 如果不考虑动画效果,也可以直接[tableView reload];
    }
}
移动UITableView的行
首先要开启编辑模式
实现UITableViewDataSource的如下方法(如果没有实现此方法,将无法换行)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

   
    // 1.取出要拖动的模型数据
    Person *p = _persons[sourceIndexPath.row];
   
    // 2.删除之前行的数据
    [_persons removeObject:p];
   
    // 3.插入数据到新的位置
    [_persons insertObject:p atIndex:destinationIndexPath.row];
   
}

 
选中UITableView的行
当某行被选中时会调用此方法(UITableViewDelegate的方法)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //取消选中某一行,让被选中行的高亮颜色消失(带动画效果)
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
 
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
// 是否允许在编辑模式下选中多行

UITableViewController
 
是UIViewController的子类,UITableViewController默认扮演了3种角色:视图控制器、UITableView的数据源和代理
UITableViewController的view是个UITablView,由UITableViewController负责设置和显示这个对象。UITableViewController对象被创建后,会将这个UITableView对象的dataSource和delegate指向UITableViewController自己
 
事件处理补充:UILable不能处理事件。因此把UILabel放在按钮上面也能点击,他会把事件传递给下一个控件处理。
 

二、修改Cell的状态
1.最好通过“修改模型数据”来修改Cell的状态

2.修改步骤
1> 修改模型数据
2> 刷新表格
* 整体刷新:reloadData(最重要)
* 局部刷新:reloadRowsAtIndexPaths:withRowAnimation:

三、UITableView常见方法
1.取消选中某一行(去掉cell选中时默认的蓝色背景)
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

2.局部刷新(仅仅刷新indexPaths数组中装着的行)
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

3.整体刷新(屏幕中的每一行都刷新)
- (void)reloadData;

4.直接删除界面上的行数(要求模型数据也要删掉对应的数量)
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

5.设置编辑模式
@property(nonatomic,getter=isEditing) BOOL editing;
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

// 注意:
不管是局部刷新,还是整体刷新,原理都是:
UITableView重新向数据源(dataSource)和代理(delegate)发送相应的消息,最终将得到的数据展示出来
 
 

UITableView

标签:

原文地址:http://www.cnblogs.com/kangshang/p/4225001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!