iOS——UITableView

一、UITableView概述

  • UITableView继承于UIScrollView,可以滚动
  • UITableView的每一条数据对应的单元格叫做Cell,是UITableViewCell的一个对象,继承于UIView
  • UITableView可以分区显示,每一个分区称为section,每一行称为row,编号都是从0开始

  • 系统提供了一个专门的类来整合section和row,叫做NSIndexPath

二、UITableView

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style

UITableView的样式:有两种:
1.UITableViewStylePlain:分区头和分区尾会停靠在tableView头部和尾部
2.UITableViewStyleGrouped:分区头/分区尾高度大于Plain样式时的高度,头尾不会停滞

行高

@property (nonatomic) CGFloat rowHeight;

分割线样式

@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle;

样式:

typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
    UITableViewCellSeparatorStyleNone,
    UITableViewCellSeparatorStyleSingleLine,
    UITableViewCellSeparatorStyleSingleLineEtched
};

分割线颜色

@property (nonatomic, strong, nullable) UIColor *separatorColor

UITableView的置顶视图

@property (nonatomic, strong, nullable) UIView *tableHeaderView;

UITableView的置顶视图

@property (nonatomic, strong, nullable) UIView *tableFooterView;

刷新UITableView

- (void)reloadData;

UITableView的头部

@property (nonatomic, strong, nullable) UIView *tableHeaderView;

UITableView的尾部

@property (nonatomic, strong, nullable) UIView *tableFooterView;  

三、UITableView协议

显示数据相关的代理

@property (nonatomic, weak, nullable) id <UITableViewDataSource> dataSource;

视图操作相关的代理

@property (nonatomic, weak, nullable) id <UITableViewDelegate> delegate;

必须实现的协议方法:
返回行数

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

返回每一行显示的cell

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

可选的协议方法(部分):
返回指定的row的行高

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

返回分区数

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

返回每一个分区头的标题

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

返回每一个分区尾的标题

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

返回指定分区头的高度

-(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

UITableView右侧的索引目录

-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView

告诉delegate选中了一个cell

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

四、UITableView重用cell的流程

重用池机制

TableView的重用机制,为了做到显示和数据分离,IOS tableView的实现并且不是为每个数据项创建一个tableCell。而是只创建屏幕可显示最大个数的cell,然后重复使用这些cell,对cell做单独的显示配置,来达到既不影响显示效果,又能充分节约内容的目的。

1.在创建UITableView之后,需要注册一个cell类,当重用池中没有cell的时候,系统可以自动创建cell

- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier;

2、系统提供了一个获取重用池中cell的方法(需要提供一个重用标识)

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

五、UITableView编辑步骤

1、让TableView处于编辑状态

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

2、协议设定
(1).确定cell是否处于编辑状态
删除或添加

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

移动

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

(2).设定cell的编辑模式(删除,添加)

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

(3).编辑状态进行提交
删除或添加

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

移动

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

六、UITableView自定义

UITableView中系统的Cell共提供了四种默认样式,分别是:

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault,
    UITableViewCellStyleValue1,
    UITableViewCellStyleValue2,

    UITableViewCellStyleSubtitle
};

但实际使用过程中,Cell样式的布局上千差万别,因此要使用自定义的UITableView。

第一步:创建一个继续UITableView的文件,然后进行单元格Cell自定义布局,实现UITableView的初始化方法:

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

第二步:确保所有的你想添加的子视图都在自定义Cell的初始化方法中创建,由于UITableView的重用机制,一个Cell在第一次创建成功并用于下一次显示的时候,不会再走初始化方法,这样可以避免子视图的重复创建

第三步:在Cell的子视图创建成功后,将子视图设置为属性,便于在UITableView的协议中给自定义视图赋值

第四步:创建一个继承于NSObject的Model类,便于提供数据,现在我们的数据提供都是存放在数组或字典中,OC中得KVC就是帮助我们将字典转换为Model类而存在的。
(1)创建一个类并继承用于NSObject
(2)添加和字典或数组对应的属性
(3)在视图控制器中将字典通过KVC为Model赋值
(4)将Model对象添加到数组中并刷新TableView
注意:字典的数据,需要在Model的.m文件加上以下这句,不然程序会崩溃

-(void)setValue:(id)value forUndefinedKey:(NSString *)key
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值