删除、插入、移动单元格的具体实例如下:


代码如下:
1 #import "ViewController.h"
2 #define NUM 20
3 typedef enum
4 {
5 deleteCell,
6 addCell,
7 moveCell,
8 }cellState;
9 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
10 @property (weak, nonatomic) IBOutlet UITableView *tableView;
11 @property (strong,nonatomic)NSMutableArray *arrayM;
12 @property (assign,nonatomic)cellState state; //对单元格要处理的状态
13 @end
14
15 @implementation ViewController
16 //删除单元格
17 - (IBAction)addCellClicked:(UIBarButtonItem *)sender
18 {
19 self.state = addCell;
20 self.tableView.editing = !self.tableView.editing;
21 }
22 //插入单元格
23 - (IBAction)deleteCellClicked:(UIBarButtonItem *)sender
24 {
25 self.state = deleteCell;
26 self.tableView.editing = !self.tableView.editing;
27 }
28 //移动单元格
29 - (IBAction)moveCellClicked:(UIBarButtonItem *)sender
30 {
31 self.state = moveCell;
32 self.tableView.editing = !self.tableView.editing;
33 }
34
35 - (void)viewDidLoad {
36 [super viewDidLoad];
37
38 //初始化
39 self.arrayM = [NSMutableArray arrayWithCapacity:NUM];
40 for(int i=0; i<NUM; i++)
41 {
42 NSString *productName = [NSString stringWithFormat:@"product-%02d",i+1];
43 [self.arrayM addObject:productName];
44 }
45
46 //设置数据源和代理
47 self.tableView.dataSource = self;
48 self.tableView.delegate = self;
49 }
50
51 #pragma mark -tableView的方法
52 //每一个section有多少个row
53 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
54 {
55 return self.arrayM.count;
56 }
57 //设置每一个单元格的内容
58 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
59 {
60 //1.根据reuseIdentifier,先到对象池中去找重用的单元格对象
61 static NSString *reuseIdentifier = @"productCell";
62 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
63 //2.如果没有找到,自己创建单元格对象
64 if(cell == nil)
65 {
66 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
67 }
68 //3.设置单元格对象的内容
69 cell.textLabel.text = self.arrayM[indexPath.row];
70 return cell;
71 }
72
73 #pragma mark -tableView的方法
74 //返回单元格编辑类型
75 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
76 {
77 if(self.state == deleteCell)
78 {
79 return UITableViewCellEditingStyleDelete; //删除
80 }
81 else if(self.state == addCell)
82 {
83 return UITableViewCellEditingStyleInsert; //插入
84 }
85 else
86 {
87 return UITableViewCellEditingStyleNone; //移动
88 }
89 }
90 //对单元格做编辑处理
91 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
92 {
93 //取出当前的单元格
94 UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
95 if(cell.editingStyle == UITableViewCellEditingStyleDelete)//删除单元格
96 {
97 //1.删除该单元格
98 [self.arrayM removeObjectAtIndex:indexPath.row];
99 //2.局部刷新表格
100 [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
101 }
102 if(cell.editingStyle == UITableViewCellEditingStyleInsert)//插入单元格
103 {
104 //1.创建产品资源
105 NSString *product = [NSString stringWithFormat:@"product-%02d",arc4random_uniform(20)];
106
107 //2.插入该数据到当前单元格
108 [self.arrayM insertObject:product atIndex:indexPath.row];
109
110 //3.整体刷新表格
111 [self.tableView reloadData];
112 }
113 }
114 //是否可以移动
115 -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
116 {
117 if(self.state == deleteCell || self.state == addCell)
118 {
119 return NO;
120 }
121 else
122 {
123 return YES;
124 }
125 }
126 //移动单元格
127 -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
128 {
129 //修改数组中元素的顺序
130 //取出数组中要移动的元素
131 NSString *item = [self.arrayM objectAtIndex:sourceIndexPath.row];
132
133 //删除原来位置的元素
134 [self.arrayM removeObjectAtIndex:sourceIndexPath.row];
135
136 //在新的位置上重新插入新的元素
137 [self.arrayM insertObject:item atIndex:destinationIndexPath.row];
138 }
139 @end
原文:http://www.cnblogs.com/daxiong520/p/4916009.html