初步学习UITableView(摘自传智播客)

本文详细介绍了UITableView的使用方法,包括数据源和代理的设置,UITableView的样式,单元格的自定义,编辑模式,行的删除和移动,以及UITableView的常用属性和方法。

#####UITableView UITableView是作为IOS中显示数据列表最常用的一个控件,继承UIScrollView,支持垂直滚动。拥有两种内置的样式,UITableViewStylePlain普通样式与UITableViewStyleGrouped分组样式。 普通样式:

分组样式:
#####数据源(dataSource)与代理(delegate) UITableView需要一个数据源(dataSource)来显示数据,UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等。没有设置数据源的UITableview只是个空壳,凡是遵守UITableViewDataSource协议的Object-c对象都可以作为UITableView的数据源。 通常都要为UITableView设置代理(delegate),以便在UITableView触发事件时作出相应的处理,比如选中了某一行。凡是遵守了UITableViewDelegate协议的Object-c对象,都可以是UITableView的代理对象。 一般情况下会让控制器充当UITableView的数据源和代理。 ######UITableViewDataSource提供的一些方法

//共分为多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
}
复制代码
//每组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
}  
复制代码
//每行显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
}
复制代码
//某一组的头部标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
}
复制代码
//某一组的尾部标题
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
}
复制代码
//某一行是否可以编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
}
复制代码
//某一行是否可以移动来进行重新排序
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
}
复制代码
//UITableView右边索引栏的内容
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
}
复制代码

#####UITableViewDelegate提供的一些方法

//选中了UITableView的某一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
复制代码
//某一行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
}
复制代码
//某一组头部的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
}
复制代码
//某一组尾部的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
}
复制代码
//某组头部显示的视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
}
复制代码
//某组尾部显示的视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
}
复制代码
//设置每一行的等级缩进(字越小等级越高)
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ 
}
复制代码

#####UITableViewCell UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowIndexPath:方法来初始化每一行 UITableViewCell是UIView的子类,内部有个默认的子视图:contentView。contentView是UITableViewCell所显示内容的父视图,并负责显示一些辅助指示视图。辅助指示视图是显示一个表示动作的图标,可以通过设置UITableViewCell的accessoryType来显示,默认是UITableViewCellAccessoryNone(不显示辅助指示图),其他值如下:

#####UITableView的contentView contentView下默认有三个子视图,其中的2个是UILabel(通过UITableViewCell的textLabel和detailTextLabel属性访问),第三个是UIImageView(通过UITableViewCell的imageView属性访问)。 UITableViewCell还有一个UITableViewStyle属性,用于决定使用contentView的哪些子视图,以及这些子视图在contentView中的位置,起样式如下:
#####UITableViewCell自定义 当系统提供的UITableViewCell样式不能满足需求时,我们可以创建UITableViewCell类并继承UITableViewCell来自定义样式,重写UITableViewCell的初始化方法,例如:

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.backgroundColor = [UIColor whiteColor];
        [self drawCell];
    }
    return self;
}
复制代码

#####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:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *identifier = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"Text %li",(long)indexPath.row];
    return cell;
}
复制代码

#####不重用UITableViewCell对象的代码片段

- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *identifier = @"UITableViewCell";
    UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"Text %li",(long)indexPath.row];
    return cell;
}
复制代码

#####UITableViewCell的常用属性 设置背景 backgroundView 设置被选中时的背景视图 selectedBackgroundView selectionStyle属性可设置UITableViewCell被选中时的背景颜色: 没有颜色 UITableViewCellSelectionStyleNone 蓝色(默认) UITableViewCellSelectionStyleBlue 灰色 UITableViewCellSelectionStyleGray #####自定义UITableViewCell 通过代码往UITableViewCell的contentView中添加子视图,在初始化方法(比如init,initWithStyle:reuseIdentifier:)中添加子控件,在layoutSubViews方法中分配子控件的位置和大小 #####UITableView的编辑模式 UITableView有个editing属性,设置为YES时,可以进入编辑模式。在编辑模式下,可以管理表格中的行,比如改变行的排列顺序,增加行,删除行,但不能修改行的内容。 #####开启编辑模式的方式 @property(nonatomic,getter = isEditing)Bool editing; -(void)setEditing:(Bool)editing animated:(Bool)animated #####删除UITableView的行 1.首先要开启编辑模式 2.实现UITableViewDataSource的如下方法:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(editingStyle == UITableViewCellEditingStyleDelete){
        //删除真实数据
        [self.data removeObjectAtIndex:indexPath.row];
        //删除UITableView中的某一行(带动画效果)
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
        //如不考虑动画效果,也可以直接
        [tableView reloadData];
    }
}
复制代码

#####移动UITableView的行 1.首先要开启编辑模式 实现UITableViewDateSource的如下方法(如果没有实现此方法,将无法换行):

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    NSInteger from = sourceIndexPath.row;
    NSInteger to = destinationIndexPath.row;
    if(from == to){
        return;
    }
     //交互数据
    [self.data exchangeObjectAtIndex:from withObjectAtIndex:to];
}
复制代码

####选中UITableView的行 当某行被选中时会调用此方法(UITableViewDelegate的方法)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //取消选中某行时,让被选行的高亮颜色消失(带动画效果)
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
复制代码

#####UITableView常用方法 -(void)initWithFrame:(CGRect)fram style:(UITableViewStyle)style 初始化一个UITableView,并且设置表格样式 -(void)reloadData 重新访问数据源,刷新界面 -(NSInteger)numberOfSections 分组的个数 -(NSInteger)numberOfRowInSections:(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)deleteRowAtIndexPath:(NSArray *)indexPath withRowAnimation:(UITableViewRowAnimation) animation. 移除indexPath范围内的所有行 @property(nonatomic, readonly)UITableViewStyle style 表格样式 @property(nonatomic, assign)id dataSource 数据源 @property(nonatomic, assign)id 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默认扮演了三种角色:视图控制器,UITableView的数据源和代理 UITableViewController的View是个UITableView,由UITableViewController负责设置和显示这个对象。UITableViewController对象被创建后,会将这个UITableView对象的dataSource和delegate指向UITableViewController自己。

转载于:https://juejin.im/post/5cb467865188251d2869994d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值