IOS开发学习笔记032-UITableView 的编辑模式

UITableView 的三种编辑模式

1、删除

2、排序

3、添加

进入编辑模式,需要设置一个参数

1 - (IBAction)remove:(UIBarButtonItem *)sender
2 {
3     NSLog(@"removed");
4     // 进入编辑模式
5     BOOL removed = !self.tableView.isEditing; //获取当前状态进行取反 
6     [self.tableView setEditing:removed animated:YES]; //设置编辑模式,并设置动画
7 }

 

1、实现界面

界面组成为两个lable标签,新建一个模型类Person保存数据,直接使用默认UITableViewCell默认的cell,设置textLable控件和detailLable控件,然后设置显示样式为 UITableViewCellStyleValue1,

两个标签并排显示。至于怎么初始化显示,我这里就不说了,可以看以往的文章。

2、删除

响应删除按钮需要实现一个方法 commitEditingStyle

 1 // 进入编辑模式,点击删除会调用这个方法,实现这个方法就可以出现向左滑动出现删除按钮
 2 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 3 {
 4     // 如果不是删除模式就退出
 5     if (editingStyle != UITableViewCellEditingStyleDelete) {
 6         return;
 7     }
 8     // 1、获取cell
 9     Person *p = _data[indexPath.row];
10     // 2、删除cell
11     [_data removeObject:p];
12     // 3、更新cell
13     //reloadRowsAtIndexPaths 使用前提就是模型数据没有改变
14     //[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; // 模型对象数据内容改变,可以用这个
15     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; // 模型对象个数改变,用这个方法
16     
17 }

3、排序

 1 // 编辑模式下得排序功能
 2 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
 3 {
 4     // 1、获得拖动对象
 5     Person *p = _data[sourceIndexPath.row];
 6     // 2、删除拖动对象
 7     [_data removeObject:p];
 8     // 3、插入拖动对象到最新位置
 9     [_data insertObject:p atIndex:destinationIndexPath.row];
10

4、添加

添加的话,可能有点麻烦,主要是把单击添加按钮的这个消息传递当方法 editingStyleForRowAtIndexPath

 这里使用设置addBtn按钮的tag来标识按钮是否按下,默认是0,按下设置为2。

按钮按下时设置tag

1 - (IBAction)add:(UIBarButtonItem *)sender
2 {
3     // 进入编辑模式
4     //isAdd = YES;
5     _addBtn.tag = 2 ; // 设置按钮tag为2
6     BOOL added = !self.tableView.isEditing;
7     [self.tableView setEditing:added animated:YES]; // 设置为编辑模式
8 }

设置显示样式

 1 // 设置每一行的样式
 2 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
 3 {
 4     if(_addBtn.tag == 2) // 添加按钮是否按下
 5         return UITableViewCellEditingStyleInsert; // 添加
 6     else if(_addBtn.tag == 0)
 7        return UITableViewCellEditingStyleDelete; // 删除
 8     else
 9         return UITableViewCellEditingStyleNone; // 默认
10 }

添加cell

 1 // 进入编辑模式,点击删除会调用这个方法,实现这个方法就可以出现向左滑动出现删除按钮
 2 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 3 {
 4     NSLog(@"editingStyle");
 5     // 如果是删除模式
 6     if (editingStyle == UITableViewCellEditingStyleDelete)
 7     {
 8         // 1、获取cell
 9         Person *p = _data[indexPath.row];
10         // 2、删除cell
11         [_data removeObject:p];
12         // 3、更新cell
13         //reloadRowsAtIndexPaths 使用前提就是模型数据没有改变
14         //[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; // 模型对象数据内容改变,可以用这个
15         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; // 模型对象个数改变,用这个方法
16         
17     }
18     // 如果是添加模式
19     else if(editingStyle == UITableViewCellEditingStyleInsert)
20     {
21         NSLog(@"insert");
22         // 1、获取cell
23         Person *p = [Person personWithName:@"personAdd" andPhone:@"123"];
24         // 2、插入cell
25         [_data insertObject:p atIndex:indexPath.row + 1];
26         // 3、更新cell
27         [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
28         [tableView reloadData];
29     }
30     
31 }

源代码

  1 //
  2 //  ViewController.m
  3 //  UITableView-编辑模式
  4 //
  5 //  Created by Christian on 15/5/26.
  6 //  Copyright (c) 2015年 slq. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 #import "Person.h"
 11 
 12 @interface ViewController () <UITableViewDataSource>
 13 
 14 {
 15     NSMutableArray *_data;
 16 }
 17 @end
 18 
 19 @implementation ViewController
 20 - (void)viewDidLoad
 21 {
 22     [super viewDidLoad];
 23     // Do any additional setup after loading the view, typically from a nib.
 24     // 初始化
 25     _data = [NSMutableArray array];
 26     for (int i = 0 ; i < 20; i ++)
 27     {
 28         Person *p = [[Person alloc] init];
 29         p.name = [NSString stringWithFormat:@"person-%d",i];
 30         p.phone = [NSString stringWithFormat:@"%d2389823",i];
 31         [_data addObject:p];
 32     }
 33     _addBtn.tag = 0;
 34 }
 35 
 36 - (IBAction)remove:(UIBarButtonItem *)sender
 37 {
 38     // 进入编辑模式
 39     _addBtn.tag = 0; // 设置添加按钮tag为0
 40     BOOL removed = !self.tableView.isEditing;
 41     [self.tableView setEditing:removed animated:YES];
 42 
 43 }
 44 
 45 - (IBAction)add:(UIBarButtonItem *)sender
 46 {
 47     // 进入编辑模式
 48     _addBtn.tag = 2 ; // 设置按钮tag为2
 49     BOOL added = !self.tableView.isEditing;
 50     [self.tableView setEditing:added animated:YES];
 51 }
 52 
 53 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 54 {
 55     return _data.count;
 56 }
 57 
 58 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 59 {
 60     // 从缓存池中读取cell
 61     static NSString *ID = @"Person";
 62     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
 63     // 如果缓存池中没有,就新建一个
 64     if (cell == nil)
 65     {
 66         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
 67     }
 68     // 设置cell数据
 69     Person *p = _data[indexPath.row];
 70     cell.textLabel.text = p.name;
 71     cell.detailTextLabel.text = p.phone;
 72     return cell;
 73 }
 74 
 75 // 进入编辑模式,点击删除会调用这个方法,实现这个方法就可以出现向左滑动出现删除按钮
 76 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 77 {
 78     NSLog(@"editingStyle");
 79     // 如果是删除模式
 80     if (editingStyle == UITableViewCellEditingStyleDelete)
 81     {
 82         // 1、获取cell
 83         Person *p = _data[indexPath.row];
 84         // 2、删除cell
 85         [_data removeObject:p];
 86         // 3、更新cell
 87         //reloadRowsAtIndexPaths 使用前提就是模型数据没有改变
 88         //[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; // 模型对象数据内容改变,可以用这个
 89         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; // 模型对象个数改变,用这个方法
 90         
 91     }
 92     // 如果是添加模式
 93     else if(editingStyle == UITableViewCellEditingStyleInsert)
 94     {
 95         NSLog(@"insert");
 96         // 1、获取cell
 97         Person *p = [Person personWithName:@"personAdd" andPhone:@"123"];
 98         // 2、插入cell
 99         [_data insertObject:p atIndex:indexPath.row + 1];
100         // 3、更新cell
101         [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
102         [tableView reloadData];
103     }
104     
105 }
106 
107 // 编辑模式下得排序功能
108 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
109 {
110     // 1、获得拖动对象
111     Person *p = _data[sourceIndexPath.row];
112     // 2、删除拖动对象
113     [_data removeObject:p];
114     // 3、插入拖动对象到最新位置
115     [_data insertObject:p atIndex:destinationIndexPath.row];
116 }
117 // 设置每一行的样式
118 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
119 {
120     if(_addBtn.tag == 2) // 添加按钮是否按下
121         return UITableViewCellEditingStyleInsert; // 添加
122     else if(_addBtn.tag == 0)
123        return UITableViewCellEditingStyleDelete; // 删除
124     else
125         return UITableViewCellEditingStyleNone; // 默认
126 }
127 
128 
129 @end

源代码参考: http://pan.baidu.com/s/1rZgGu

原文地址:https://www.cnblogs.com/songliquan/p/4523960.html