UITableView的那些事

本文详细介绍了UITableView的创建步骤,包括遵守代理和数据源协议,设置UITableView样式,并讲解了cell复用机制、代理方法、响应相关及自定义cell的方法。此外,还展示了如何实现下拉刷新和上拉加载功能,通过导入MJRefresh框架并添加刷新组件。
摘要由CSDN通过智能技术生成

UITableView是什么:

1、UITableVIew是最常用的UI控件,基本各大APP都是基于tableview的设计。

2、UITableView经常用于列表展示,然后自定义cell类型来适用不同功能。

UITableView的故事:

UITableView的创建:

第一步:遵守代理和数据源协议
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
第二步:创建一个UITableView
  • style参数有三种,对应三种UITableView(根据需要设置):

1、UITableViewStylePlain // 不分组

2、UITableViewStyleGrouped, // 分组且分组的部分以直角嵌入

3、UITableViewStyleInsetGrouped //分组且分组的部分以圆角嵌入

// 创建UItableView,style选择Grouped或Plain,这里我们以Grouped为例
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20) style:UITableViewStyleGrouped];
    // 声明 tableView 的代理和数据源
    tableView.delegate = self;
    tableView.dataSource = self;
    // 添加到 view 上
    [self.view addSubview:tableView];
第三步: 设置tableview的数据源,实现下列方法

style为UITableViewStyleGrouped和UITableViewStyleInsetGrouped需要设置如下:

// tableView 中 Section 的个数(
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   
    return 6;
}

// 每个 Section 中的 Cell 个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return 3;
}
  • cell的类型有如下4种,依次对应下图:

1、UITableViewCellStyleDefault

2、UITableViewCellStyleValue1

3、UITableViewCellStyleValue2

4、UITableViewCellStyleSubtitle

4中类型依次对应如下:

  • cell复用分两种(cell复用机制下面讲):

1、当cell==nil时,手动创建cell

  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifying];

2、 通过注册cell的方式,由表视图自己创建cell

当cell==nil时,即缓存池中没有的时候,会根据注册的类型自动创建cell【推荐】

     [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifying];//注册cell

// 设置每个 Cell
-(UITableViewCell 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值