博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【UIKit】UITableView 6 编辑模式
阅读量:6958 次
发布时间:2019-06-27

本文共 5401 字,大约阅读时间需要 18 分钟。

 

【1】拖动好界面

【2】设置协议,数据源

【3】代码

1.声明可变数组,用来存放所有数据对象

 

 

 

 

 

 

 

 

@interface ViewController ()@property(nonatomic,strong)NSMutableArray *mydata;@end

2.初始化数据【创建30个对象数据】

- (void)viewDidLoad{    [super viewDidLoad];    self.mydata=[NSMutableArray array];    for(int i =0;i<30;i++)    {        NSString *str=[NSString stringWithFormat:@"it-%d",i];        [self.mydata addObject:str];    }}

3.设置返回行数

#pragma mark -返回行数-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.mydata.count;}

 


 

【删除方法】

1.添加固定方法,显示一个cell就调用该方法,优化性能

#pragma mark 每当有以个cell进入视野范围内就会调用,返回当前这行显示的cell-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 1. 用static 修饰的 局部变量,只会初始化一次    static NSString *ID=@"Cell";    // 2. 拿到一个标识先去缓存池中查找对应的Cell    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];    // 3. 如果缓存池中没有,才需要传入一个标识创建新的Cell    if(cell==nil)        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];    // 4.覆盖数据    cell.textLabel.text=self.mydata[indexPath.row];    cell.detailTextLabel.text=@"哈哈哈";    return cell;}

 

2.这个方法是提交编辑时候调用【如视频,点击Delete,就会调用下面的方法】

#pragma mark 提交编辑时调用// 一点击删除,就会调用这个方法/ 左滑动删除-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle  forRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"-------commit ------%d",indexPath.row);        // 删除数据        // 1. 更改数据(删除本行数据)    [self.mydata removeObjectAtIndex:indexPath.row];// 传入一个位置,将这个位置上的数据删除        // 2.刷新数据   // [tableView reloadData];// 数据刷新,全局刷    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];    }

 

3.调用代理方法中的监听事件,【删除】信息内容的修改

[开启编辑状态]

self.tableView.editing=YES;
// 编辑状态
[self.tableView setEditing:YSE animated:YES];
// 带有动画效果

 

#pragma  mark -代理方法
#pragma mark -监听item的点击#pragma  mark 删除-(IBAction)remove{    bool edt=self.tableView.editing; //取得现在编辑状态是否开启    //按一下开启编辑状态    // 没有动画效果    //  self.tableView.editing=YES;  // 编辑状态    [self.tableView setEditing:!edt animated:YES];  // 带有动画效果}

 

 


 

【增加方法】

【1】.增加方法与删除方法类似,先创建“+”按钮的代理方法,连线

【2】.代码

  1.添加数据用的方法: [self.mydata insertObject:[添加的内容] atIndex:[添加到第几行]];

      2.刷新数据: inSection 表示是组号,indexPathForRow表示的是行号

  1)获取到一个数组
   NSIndexPath *newPath=[NSIndexPath indexPathForRow:[行号] inSection:[组别号]];    
  2)传入一个数组
   [tableView insertRowsAtIndexPaths:@[newPath] 【上面的数组】withRowAnimation:UITableViewRowAnimationTop【动画效果】];
#pragma mark 提交编辑时调用(删除和添加方法都会调用该方法)// 一点击“+”就会调用这个方法-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{  // 增加数据        // 1. 更改数据(删除本行数据)        [self.mydata insertObject:@"新添加数据"atIndex:indexPath.row+1];        // 2.刷新数据        NSIndexPath *newPath=[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];         [tableView insertRowsAtIndexPaths:@[newPath] withRowAnimation:UITableViewRowAnimationTop];    }

 

[tableView reloadRowsAtIndexPaths:<#(NSArray *)#>【指定行】 withRowAnimation:<#(UITableViewRowAnimation)#>]
刷新指定行的数据(个数不变)
[tableView deselectRowAtIndexPath:<#(NSIndexPath *)#> 【指定行】 animated:<#(BOOL)#>【动画效果】]
删除指定行(删除后个数与数据个数保持一致)
[tableView insertRowsAtIndexPaths:@[newPath]【指定行要求数组】  withRowAnimation:UITableViewRowAnimationTop];
插入新的行

 

3.加入add方法

-(IBAction)add{    // 取出当前的编辑状态    bool edt=self.tableView.editing;        // 设置调用方法,方便开启编辑模式判断是否是添加    self.tableView.tag=UITableViewCellEditingStyleInsert;        // 开启编辑模式    [self.tableView setEditing:!edt animated:YES];}

4.通过上面【add】方法与【remove】方法中设置的Tag,可以获取调用移除还是增加方法。然后使用这个编辑模式。

#pragma  mark -代理方法#pragma mark 当tableView开启编辑模式就会调用-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    // 通过调用add 和remove方法中的tag变量,来确定是要添加还是删除。    return tableView.tag;}

5.如果增加的话,最终会调用一下下面的方法,但是上面已经写了

#pragma mark 每当有以个cell进入视野范围内就会调用,返回当前这行显示的cell-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}

  


 

【排序模式】

1.增加代码

sourceIndexPath :当前需要移动的行
destinationIndexPath :移动目标的行
#pragma mark 如果实现了这个方法,就有排序功能-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ // 当从某一行一动到另外一行时,会调用该方法。    NSLog(@"%d-->%d",sourceIndexPath.row,destinationIndexPath.row);// 这行可以说明调用步骤      //  如果直接使用这样的方法,只是界面进行改变,数据没有改变,违反了MVC所以要用以下方法        // 1.取出即将要删除的数据    NSString *data =self.mydata[sourceIndexPath.row];        // 2.删除要一动的那一行    [self.mydata removeObject:data];        // 插入之前删除的数据到某一行    [self.mydata insertObject:data atIndex:destinationIndexPath.row];}

 

 


 

 

【总结】

1.增加\删除

一、删除\添加: 1.开启编辑模式 [self.tableView setEditing:YES animated:YES];  2.实现数据源的某个方法 tableView:commitEditingStyle:forRowAtIndexPath:  3.下面方法的返回值决定编辑模式是添加还是删除 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

2.排序

二、排序: 实现下面的方法即可: tableView:moveRowAtIndexPath:toIndexPath:

3.刷新界面

三、4个刷新UI界面的方法 1.添加新的行 [tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop];  2.删除指定的行 [tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop];  3.局部刷新指定的行 [tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop];  4.整体刷新所有的行 [tableView reloadData];

 

 

转载于:https://www.cnblogs.com/madeininfi/p/3670365.html

你可能感兴趣的文章
C#中结构函数和析构函数的用法
查看>>
CC2540串口输出调试功能
查看>>
px,dp,sp三者的转换
查看>>
forward_list详解
查看>>
day13_H5_CSS_1
查看>>
如何在《救赎之路》中使用CPU粒子效果
查看>>
教你50招提升ASP.NET性能(一):缓存是最后的手段
查看>>
微信小程序开发填坑
查看>>
正则表达式基础
查看>>
Kakfa揭秘 Day5 SocketServer下的NIO
查看>>
网易面试总结(2014.12.31)
查看>>
runloop 和 CFRunLoop - 定时器 - NSTimer 和 GCD定时器
查看>>
【Android】3.6 地图基本控制方法
查看>>
浏览器左下角提示网页上有错误解决方法
查看>>
Yii2 配置yii2-redis扩展
查看>>
html5 标签书写的规范
查看>>
文件包含漏洞
查看>>
java导出Excel工具类
查看>>
malloc calloc realloc,new区别联系以及什么时候用
查看>>
用ES6巧妙的解决传统面试中的算法小问题!
查看>>