【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的一个子类);

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

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;

实现函数如下:

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

其他可选方法大家去详细参看ios开发者文档;

2)UITableViewDelegate

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

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

- (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的某一行的移动、编辑、删除等操作了。有机会再写。


原文地址:https://www.cnblogs.com/riskyer/p/3278432.html