zoukankan      html  css  js  c++  java
  • UITableView 总结(一)

    UITableView初始化:表格控件在创建时,必须指定style.

    UITableView的分以下为两种

    UITableViewStylePlain:平板格式             

    UITableViewStyleGrouped:分组格式

     1 - (UITableView *)tableView
     2 {
     3     if (_tableView == nil) {
     4         // 表格控件在创建时,必须指定style
     5         _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
     6         
     7         _tableView.dataSource = self; // 数据源方法
     8         _tableView.delegate = self;  // 代理方法
     9         
    10         [self.view addSubview:_tableView];
    11         
    12     }
    13     return _tableView;
    14 }

    UITableView的数据源方法:

    必须加上_tableView.dataSource = self;这句代码,并且遵守UITableViewDataSource协议才能使用数据源方法。

    数据源协议中以下两个方法是@required必须实现

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

    作用:返回每一个分组的数据总数,可以根据section得到每一个分组的总数,如下:

    1 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    2 {
    3     // 找到group
    4     CarGroup *group = self.carGroups[section];
    5     return group.title;
    6 }

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

    作用:告诉表格控件,每一行cell单元格的细节,如下:

     1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     2 {    // 可重用标示符
     3     NSString *ID = @"Cell";
     4     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];// 在缓存池中查找
     5     
     6     // 如果没有找到,就创建一个
     7     if (cell == nil) {
     8         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
     9     }
    10     
    11     // 设置cell内容
    12     // 1> 取出数据模型
    13     CarGroup *group = self.carGroups[indexPath.section];
    14     Car *car = group.cars[indexPath.row];
    15     // 2> 设置数据
    16     cell.imageView.image = car.image;
    17     cell.textLabel.text = car.name;
    18     
    19     return cell;
    20 }

    参数(NSIndexPath *)indexPath里面包含两个信息<section, row>,其中section是当前cell所在的组数,row是行数。

    初始化cell的时候先利用ID在缓存池中查找是否有可重用的cell,如果有继续,没有就创建一个,创建时;

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];

    其中reuseIdentifier:ID 是为了下次使用cell时,方便在缓存池中查找。

    initWithStyle:UITableViewCellStyleSubtitle,cell的Style分为以下四种:

    UITableViewCellStyleDefault,       默认类型 标题 + 可选图像

     UITableViewCellStyleValue1,        标题 + 明细(在标题后面) + 图像

     UITableViewCellStyleValue2,        不显示图像,标题 + 明细

     UITableViewCellStyleSubtitle    标题 + 明细(在标题下面) + 图像

    其中cell的右侧可以设置,,利用cell的accessoryType,在上面方法中设置,例如:

    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    cell的accessoryType分为以下四种:

    1> UITableViewCellAccessoryDisclosureIndicator: 箭头,可以提示用户当前行是可以点击的,通常选中行回跳到新的页面

    2> UITableViewCellAccessoryCheckmark: 对号,提示用户该行数据设置完毕,使用的比较少

    3> UITableViewCellAccessoryDetailButton: 按钮,通常点击的按钮可以做独立的操作,不会影响行的选中,例如alertView

     4> UITableViewCellAccessoryDetailDisclosureButton: 按钮+箭头,各自操作,按钮不会影响行的选中

    通常 accessoryType提供的类型不能满足时,才会使用自定义控件,但是需要自行添加监听方法,通常用在自定义cell,不要写在视图控制器中,自定义控件的事件触发同样不会影响表格行的选中,例如添加UISwitch,但监听事件需要自行添加。

    1 // 右侧指定自定义视图
    2     UISwitch *switcher = [[UISwitch alloc] init];
    3     [switcher addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    4     cell.accessoryView = switcher;

    运行效果:

    当右侧按钮不是自定义的时候,右侧按钮的监听方法如下,即需要实现下面的数据源方法

    - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;

     

    其他几个常用的数据源方法如下:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; //作用:返回分组总数

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;  // 设置每组的标题  

    - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;  // 设置分组的描述,底部文字

    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;// 设置右侧的索引列表

    1 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    2 {
    3     return [self.carGroups valueForKeyPath:@"title"];
    4 }

    如图:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;// 选中某一行是触发

    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath; // 取消选中某一行时触发

     

    UITableView的代理方法:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; // 设置行高

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; // 设置标题高度

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; // 设置底部高度

    其中行高也可以用_tableView.rowHeight = 100设置,以下对比

    _tableView.rowHeight:  效率高,适用于所有的行高一致

     代理方法指定行高:         效率差,适合于每一个行高不一样,能够让表格更加灵活。

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    160628、利用Oracle rownum让表排序字段值连续
    160627、你想知道的关于JavaScript作用域的一切
    160624、Spark读取数据库(Mysql)的四种方式讲解
    160623、理解 Promise 的工作原理
    160622、详解JavaScript变量提升
    160621、Java注解教程及自定义注解
    详解JavaScript数组过滤相同元素的5种方法
    box-shadow
    Axios 中文说明
    一步一步学Vue(九) 路由元数据
  • 原文地址:https://www.cnblogs.com/liuqblog/p/4865261.html
Copyright © 2011-2022 走看看