1 #import "ViewController.h"
2
3 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
4 /**
5 * 存储数据的数组
6 */
7 @property(nonatomic,strong) NSMutableArray *studetsNameArray;
8 /**
9 * tabView的表视图展示数据
10 */
11 @property(nonatomic,strong) UITableView *tableView;
12 @end
13
14 @implementation ViewController
15
16 - (void)viewDidLoad {
17 [super viewDidLoad];
18 // Do any additional setup after loading the view, typically from a nib.
19
20 UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
21 tableView.dataSource = self;
22 tableView.delegate = self;
23 self.tableView = tableView;
24 self.studetsNameArray = [NSMutableArray arrayWithObjects:@"张秀洁",@"麻振辉",@"王 振",@"刘润宁",@"王玲玲",@"原溢锴",@"余清平",@"刘文学",@"李日清",@"李小康",@"焦焕然",@"蔺宇彬",@"韩振宇",@"刘永庆",@"张 涛",@"钟富昌",@"刘梦君",@"吴岳恒",@"任东强",@"温 哲",@"张明亮",@"孙君堂",@"岳邵军",@"燕 慈",@"杜湛军",@"石 鑫",@"毛艺霖",@"王自发",@"郭 宁",@"张佳翔",@"朱 磊",@"孟德峰",@"李 雅",@"王 俊",@"李 曼",@"孟方乐",@"卢 浩",@"刘其中",@"彭晨晨",@"蒋宗涛",@"高存昊",@"邱纪生", nil];
25
26
27 [self.view addSubview:self.tableView];
28
29 #pragma mark - 设置编辑的流程
30 //第一步:打开tableView的编辑状态
31 [self.tableView setEditing:YES animated:YES];
32
33 //第二步:设置编辑相关的代理
34 /*
35 1.确定是否处于编辑状态
36 2.设置编辑的样式
37 3.提交编辑状态处理数据
38 以上均是代理方法
39 */
40 //将naVC的右侧按钮设置为编辑按钮(每一个控制器里都提供了一个编辑的按钮)
41 self.navigationItem.rightBarButtonItem = self.editButtonItem;
42
43
44
45
46
47 }
48 #pragma mark -设置tableView的移动代理方法
49 //确定每一行是否可以移动
50 -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
51 {
52 return YES;
53 }
54 //处理移动的位置,还有相关的数据
55 -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
56 {
57 //取出原本位置的相关原数据
58 NSString *name = [self.studetsNameArray objectAtIndex:sourceIndexPath.row];
59 //删除原本位置的相关数据
60 [self.studetsNameArray removeObjectAtIndex:sourceIndexPath.row];
61 //将删除的数据添加到目的地数据里
62 [self.studetsNameArray insertObject:name atIndex:destinationIndexPath.row];
63
64 }
65 #pragma mark -打开tableView的编辑状态
66 -(void)setEditing:(BOOL)editing animated:(BOOL)animated
67 {
68 //父视图的交互关闭掉了,子视图还能响应吗?下面的写法只能交互一次,BOOL值只能改变一次
69 //[super setEditing:YES animated:YES];
70 //正确的写法是下面的
71 [super setEditing:editing animated:animated];
72 //调整编辑按钮在编辑和完成状态下进行切换
73 [_tableView setEditing:editing animated:animated];
74 }
75
76 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
77 {
78 return self.studetsNameArray.count;
79 }
80 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
81 {
82 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reusable"];
83 if (cell == nil) {
84 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reusable"];
85 }
86 cell.textLabel.text = [self.studetsNameArray objectAtIndex:indexPath.row];
87 return cell;
88 }
89
90 #pragma mark - 设置编辑的代理
91 //1.是否处于编辑的状态
92 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
93 {
94 NSString *name = [self.studetsNameArray objectAtIndex:indexPath.row];
95 if ([name isEqualToString:@"吴岳恒"]) {
96 return NO;
97 }
98 return YES;
99 }
100
101 //2.设置编辑的样式
102 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
103 {
104 //也可以设置多选状态 delete | insert 即可插入又可以删除
105 return UITableViewCellEditingStyleDelete;
106 }
107
108 //3.提交编辑状态,处理相关的数据
109
110 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
111 {
112 //如果编辑状态处于删除状态,需要将此条数据删除
113 if (editingStyle == UITableViewCellEditingStyleDelete) {
114 //删除与之对应的数据,(先删除数据然后再处理视图)
115 [self.studetsNameArray removeObjectAtIndex:indexPath.row];
116 //处理视图的方式有两种
117 //1.根据indexPath删除具体的某一行
118 //[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
119 //2.直接使视图重新加载数据
120 [tableView reloadData];
121 }
122 }
123 - (void)didReceiveMemoryWarning {
124 [super didReceiveMemoryWarning];
125 // Dispose of any resources that can be recreated.
126 }
127
128 @end