ios控件UITableview自己总结

如何使用UITableview,需要掌握几个要点

遵守协议

tableview所属的控制器必须遵守2个协议,一是UITableViewDataSource,数据源协议,二是UITableViewDelegate,有关代理的协议。同时,必须设置数据源对象、代理对象 。

数据源

datasource,是一个属性。任何oc类型的对象都可以成为它的数据源(必须遵守协议)。只有遵守协议才能使用自带方法。需要数据源来展示数据,一般将tableview的数据源设为控制器本身。

方法

  • 数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

此方法告诉tableview需要设置多少个数据组。一般分为单组/多组数据,当
return值为 1或没有设置此方法时,默认组数为1,即在tableview中只设置一个组;return值n大于等于1时,表示在tableview中设置n个组。从0开始,0表示第一组

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

此方法告诉tableview对应的组有多少个数据行。需要对每组依次进行设置,return几就表示几行。
ps:当tableview要展示数据时,会自动调用数据源方法

  • 代理方法
    主要作用是监听对tableview的点击/选中事件,主要包括以下几个方法:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

告诉tableview每一行显示什么内容—— 每一行都是一个UITableViewCell或其子类,故只需要返回一个cell即可。其中的参数indexPath
indexPath.section 表示组(从0开始
indexPath.row 表示行(从0开始
另外,给cell添加数据也是在此方法体中添加。

  • 注册
    在viewDidLoad:方法中注册。要注册什么类型的cell(自定义cell?)就在参数中给出类名。
[self.mytableView registerClass:[ShopCell class] forCellReuseIdentifier:@"shopID"]

说明:使用注册的方式创建的tableView,只有textLabel一个属性,如果还需要其他控件,就需要自定义cell。

  • 非注册形式
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
 if (cell == nil) {
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
   }

说明:以上是必要声明的方法,缺一不可,这个过程是tableview展示数据的过程

  • 其他常用方法:
    返回选中的行号
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

设置每一组的组头标题

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
}

设置每一组的组尾标题

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
}

选中/点击某行cell时会调用

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     //设置 取消点击后选中cell
    [tableView deselectRowAtIndexPath:indexPath animated:YES];   
}

取消选中某行cell会调用 (当我选中第0行的时候,如果现在要改为选中第1行 - >会先取消选中第0行,然后调用选中第1行的操作)

-(void)tableView:(nonnull UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    NSLog(@"取消选中 didDeselectRowAtIndexPath row = %ld ", indexPath.row);
}

数据源方法 cell左滑删除,并调用,进行事件处理(删除)

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.carArray removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

代理方法,返回左滑时,返回右边删除按钮的文字

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return @"删除";
}

PS:以上两个方法往往同时出现,实现左滑删除。
添加自定义的左滑出现按钮(会覆盖掉上面两个方法)

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    //新建左滑出现的按钮
    UITableViewRowAction *actBtn1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //点击后做什么
    }];
    actBtn1.backgroundColor = [UIColor orangeColor];
    
    UITableViewRowAction *actBtn2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"关注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //点击后做什么
        self.tableView.editing = NO;   //向右退回按钮(隐藏)
    }];
    return @[actBtn1,actBtn2];   //返回按钮数组
}

常用属性

  • 对于整个tableview来说,包括

样式(单组或多组)
UITableViewStylePlainUITableViewStyleGrouped
contentView,每一行就是一个contentview
行高(所有行)rowHeight
滚动条颜色indicatorStyle= UIScrollViewIndicatorStyleWhite;
分割线
-颜色separatorColor
设为clearColor相当于取消系统分割线
-样式separatorStyle

UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine,(默认)
UITableViewCellSeparatorStyleSingleLineEtched

头部的view
背景色backgroundColor
进入编辑状态
self.tableView.editing = YES;
[self.tableView setEditing:YES animated:YES]; (带动画)
编辑状态下可多选
allowsMultipleSelectionDuringEditing = YES
当前已选中的行的索引:indexPathForSelectedRows

  • 对于组来说,包括

组头标题
组头高sectionHeaderHeight
组尾标题
组尾高sectionFooterHeight

  • 对于行(即一个contentview)来说
    包含以下3个是固有子控件
    imageView、textLabel、detailTextLabel
    右侧视图accessoryView
    行高(某一行)
    背景色backgroundColor
    选中时的样式selectionStyle
    非编辑状态下是否可以选中allowsSelection(默认YES)
    是否可以选中多行allowsMultipleSelection(默认NO)
    可选风格:selectionStyle = UITableViewCellSelectionStyleDefault
    拿到某行cell
PDtableViewCell *cell = [self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

右端提示

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

UITableviewCellStyle(创建时设置,initWithStyle:)

UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle

数据

分为多组数据、单组数据。每一组包含若干行数据。
一般的做法是将数据独立出来成为一个模型(数据类),然后从模型中引入数据。

数据源方法补充

1. numberOfSectionsInTableView:------------设置表格的组数
2. tableView:numberOfRowInSection:--------设置每个组有多少行
3. tableView:cellForRowAtIndexPath:-------设置单元格显示的内容
4. tableView:titleForHeaderInSection:------设置组表的头标签视图
5. tableView:titleForFooterInSection:-------设置组表的尾标签视图
6. tableView:canEditRowAtIndexPath:----设置单元格是否可以编辑
7. tableView:canMoveRowAtIndexPath:--设置单元格是否可以移动
8. tableView:sectionIndexTitleForTableView:atIndex:-------设置指定组的表的头标签文本
9. tableView:commitEditingStyle:forRowAtIndexPath:----------编辑单元格(添加,删除)
10. tableView:moveRowAtIndexPath:toIndexPath-------单元格移动

代理方法补充

1. tableView: willDisplayCell: forRowAtIndexPath:----------设置当前的单元格
2. tableView: heightForRowAtIndexPath:---------设置每行的高度
3. tableView:tableView heightForHeaderInSection:-----------设置组表的头标签高度
4. tableView:tableView heightForFooterInSection:-----------设置组表的尾标签高度
5. tableView: viewForHeaderInSection:----------自定义组表的头标签视图
6. tableView: viewForFooterInSection: ----------自定义组表的尾标签视图
7. tableView: accessoryButtonTappedForRowWithIndexPath:-----------设置某个单元格上的右指向按钮的响应方法
8. tableView: willSelectRowAtIndexPath:---------获取将要选择的单元格的路径
9. tableView: didSelectRowAtIndexPath:---------获取选中的单元格的响应事件
10.tableView: tableView willDeselectRowAtIndexPath:------------获取将要未选中的单元格的路径
11. tableView: didDeselectRowAtIndexPath:-----------获取未选中的单元格响应事件
12.tableView: willDisplayCell:--------------//当cell将要显示时调用

数据的刷新

只要修改、添加、删除模型,然后刷新数据即可。处理的对象是模型,只要模型中的数据变了,tableview的样子就会改变。所以,修改模型才是本质,不要采用直接拿到某行cell进行删除、添加cell、改变cell的内容的做法。

全局

[self.myTableView reloadData];     (全局刷新,整个tableview的数据都重新刷新)
  • 数据刷新,然后tableview就会重新调用数据源方法

局部

    NSArray *indexPs = @[[NSIndexPath indexPathForRow:0 inSection:0]];
    [self.myTableView reloadRowsAtIndexPaths:indexPs withRowAnimation:UITableViewRowAnimationMiddle];
只刷新某些行, indexPs指所要刷新的行 数组,指定某组某行。

注意:reloadRowsAtIndexPaths方法使用前提是,保证模型数组个数不变。因此,这个方法不适用于添加数据的情况。对于添加操作,用insertRowsAtIndexPaths方法,添加指定的行。

    NSArray *indexPs = @[[NSIndexPath indexPathForRow:0 inSection:0]];
    [self.myTableView insertRowsAtIndexPaths:indexPs withRowAnimation:UITableViewRowAnimationLeft];

删除操作后的刷新(带动画)

//参数中指明所要删除的行(数组)
[self.myTableView deleteRowsAtIndexPaths:。。。withRowAnimation:UITableViewRowAnimationAutomatic];

关于UITableviewCell

每一个cell包含3个子控件(属性),即,ImageView,textlabel,detiltextlabel
创建:

 UITableViewCell *cell = [[UITableViewCell alloc]init];//普通创建
 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];//带有detiltext

你可能感兴趣的:(ios控件UITableview自己总结)