iOS知识学习】_UITableView简介

UITableView在iOS中估计是用的最多的控件了吧,几乎每个app都会用到。

一、它是一个非常重要的类来在table中展示数据。

1、是一个一维的表;

2、是UIScrollView的一个子类;

3、表可以是静态的或者动态的;

4、通过dataSource 协议和 delegate 协议可以实现很多的个性化定制;

5、即便拥有大量数据也非常有效率。

二、几种UITableView

1、Plain或者Grouped风格


左边是plain风格,就是一行一行的,右边是grouped风格,一块一块的。


这是plain风格的图片


上图是grouped风格

可以看到里面可以设置Table Header Section Header 、Section Footer等属性,

TableHeader、Section Header SectionFooter、Table Footer可以不设置,默认为空;

Section的数值不设置的话默认为1,

2、静态的或者动态的;

3、被分割为一个个section或者不分割


左图是没有section的,默认就是一个section,右图明显可以看出有几块,每一块就是一个section。

三、UITableView协议

一个UITableView需要继承两个协议:delegate 和 dataSource

delegate:控制table如何展示出来;

dataSource:提供在cell里展示的数据;

注:UITableViewController自动设置为UITableView的delegate和dataSource。

1)UITableViewDataSource

    - (UITableViewCell *)tableView:(UITableView *)sender cellForRowAtIndexPath:(NSIndexPath *)indexPath{ }

    这是一个静态方法,作用就是展示index在某一行的cell,该方法返回值是UITableViewCell(UIView的一个子类);

  大括号里的实现内容如下:

  1. UITableViewCell *cell; 
  2. cell = [self.tableView dequeueReusableCellWithIdentifier:@“My Table View Cell”]; 
  3. if (!cell) { 
  4. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
  5. reuseIdentifier:@“My Table View Cell”]; 
  6. cell.textLabel.text = [self getMyDataForRow:indexPath.row inSection:indexPath.section]; 
  7. return cell; 
UITableViewCell *cell;
cell = [self.tableView dequeueReusableCellWithIdentifier:@“My Table View Cell”];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:@“My Table View Cell”];
}
cell.textLabel.text = [self getMyDataForRow:indexPath.row inSection:indexPath.section];
return cell;

该协议可以让用户设置该tableView有多少个Section和多少行,Section的默认值为1;

实现函数如下:

  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)sender; 
  2. - (NSInteger)tableView:(UITableView *)sender numberOfRowsInSection:(NSInteger)section; 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)sender;
- (NSInteger)tableView:(UITableView *)sender numberOfRowsInSection:(NSInteger)section;
其他可选方法大家去详细参看ios开发者文档;

2)UITableViewDelegate

delegate主要是用来控制UITableView是如何显示的,

当tableView的某一行被选中以后,下面的函数将被执行:

  1. - (void)tableView:(UITableView *)sender didSelectRowAtIndexPath:(NSIndexPath *)path 
  2. // go do something based on information 
  3. // about my data structure corresponding to indexPath.row in indexPath.section 
- (void)tableView:(UITableView *)sender didSelectRowAtIndexPath:(NSIndexPath *)path
{
// go do something based on information
// about my data structure corresponding to indexPath.row in indexPath.section
}

其他delegate的函数大家自己参考代码。

这边就先不写tableView的某一行的移动、编辑、删除等操作了。有机会再写。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值