• ios晋级之路-tableView数据源方法详解


    好像很久没有更新了,不过搞开发是一个长久学习的一个过程!加油

    今天小小的研究了一下tableView的数据源方法

    ios 9.0的官方文档有这么一句话,大致意思是数据源方法是通过协议像tableView对象传递数据模型,并且传递了所需要的标示图信息(这里包括section row的一些配置)和对表视图的修改。这是对UITableViewDataSource的一个总体概述。

    数据源方法大体分为三大类

    1.配置tableView

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

    2)- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableVIew

    3)- (NSInteger)tableView:(UITableVIew*)tableView numberOfRowInSection:(NSInteger)section

    以上三个方法是最基本的也是必须实现的数据源方法 分别是组数、组内的行数、和每行的Tableviewcell这里不详细解释了

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

    这个方法是在设置了UITableViewStylePlain才会有效的,返回值是一个字符串数组比如这里我们返回

    @[@"A",@"B",@"C",@"D",@"E"]

    这里会显示在上图右侧,也就是这个索引。

    5)- (NSInteger)tableView:(UITableView*)tableView sectionForSecctionIndexTitle:(NSString*)title atIndex:(NSInteger)index

    首先这个方法是配合方法4)的,是在点击右侧索引后执行的,所以要先实现方法4)参数title是当前点击索引的标题(例如点击A 那返回的就是A咯)参数index返回的是点击索引的编号,返回值是一个整形数代表这点击索引之后跳转的位置如果返回1那么按照上图section的header或者footer(这个要你自己设置的以下有设置header或footer的代理方法)就会出现到屏幕上,这里一般我们直接返回index,起到一个查找的作用。

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

    这个方法是设置sectionHeader的,section是当前对应的组的编号,header是现实在每个组的最上面的,相反下面这个方法Footer是现实在这个组最下面的

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

    这个方法上面已经解释过了

    2.插入或者删除tableView中的行

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

    控制是否出现删除按钮,返回Yes出现,No不出现,这个方法如果不实现,只要实现了方法2)则默认返回Yes如右图

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

    点击删除按钮时调用该方法(具体怎么删除由自己操作)

    3.重新整理table的行

    注意如果想移动行的话要先实现如下方法

    [_tableView setEditing:YES animated:YES];

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

    告诉数据源这个行是否可以被移动

    2)

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

    当行移动的时候后,告诉数据源你从哪一行移动到了另一行。

  • 相关阅读:
    常见算法:C语言求最小公倍数和最大公约数三种算法
    java数据结构
    创建与删除索引
    Delphi 2007体验!
    wxWindows
    Android中WebView的相关使用
    IAR FOR ARM 各版本号,须要的大家能够收藏了
    [AngularJS] $interval
    [ES6] 10. Array Comprehensions
    [ES6] 09. Destructuring Assignment -- 2
  • 原文地址:https://www.cnblogs.com/fanxinguu/p/4863079.html
Copyright © 2020-2023  润新知