1 #import "RootViewController.h"
2 #import "RootView.h"
3 #define kColor arc4random() % 256 / 255.0
4 @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
5 @property (nonatomic, strong) RootView *rootView;
6 // 声明大数组, 用来存放所有学生的信息
7 @property (nonatomic, strong) NSMutableArray *allDataArray;
8 // 声明编辑样式的属性
9 @property (nonatomic, assign) UITableViewCellEditingStyle style;
10
11 @end
12
13 @implementation RootViewController
14
15 - (void)loadView
16 {
17 self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
18 self.view = self.rootView;
19
20 }
21
22 - (void)viewDidLoad {
23 [super viewDidLoad];
24 // Do any additional setup after loading the view.
25 // 设置代理
26 self.rootView.tableView.dataSource = self;
27 self.rootView.tableView.delegate = self;
28 // 设置数据
29 [self handleData];
30 // 处理导航栏
31 self.title = @"管理";
32 self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
33 // 添加右按钮
34 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(rightBarButtonItemAction:)];
35 // 添加左按钮
36 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftBarButtonItemAction:)];
37
38 }
39
40 // 设置数据
41 - (void)handleData
42 {
43 // 1. 初始化大数组
44 self.allDataArray = [NSMutableArray array];
45 // 2. 定义三个数组存放每一组学生的姓名
46 NSMutableArray *array1 = @[@"阿福", @"曲国伟", @"小楠", @"老司机", @"波波", @"唱吧小樽"].mutableCopy;
47 NSMutableArray *array2 = @[@"一打", @"mbBoy", @"lida", @"BoomSky", @"正能量"].mutableCopy;
48 NSMutableArray *array3 = @[@"孙超", @"傲然", @"MBBoy", @"🐟神", @"雷🐟"].mutableCopy;
49 // 将所有学生存放在大数组中
50 [self.allDataArray addObject:array1];
51 [self.allDataArray addObject:array2];
52 [self.allDataArray addObject:array3];
53 }
54
55 //设置分区个数
56 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
57 {
58 return self.allDataArray.count;
59 }
60
61 // 设置每个分区的行数
62 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
63 {
64 return [self.allDataArray[section] count];
65 }
66
67 // 返回cell对象
68 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
69 {
70
71 // 提到UITableView,就必须的说一说NSIndexPath。UITableView声明了一个NSIndexPath的类别,主要用 来标识当前cell的在tableView中的位置,该类别有section和row两个属性,前者标识当前cell处于第几个section中,后者代 表在该section中的第几行。
72 static NSString *identifier = @"cell";
73 // 1. 从重用池查找cell
74 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
75 // 2. 判断,如果没有找到创建cell对象
76 if (!cell) {
77 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
78 }
79 // 3. 设置cell数据
80 NSArray *array = self.allDataArray[indexPath.section];
81 cell.textLabel.text = array[indexPath.row];
82 //cell.backgroundColor = [UIColor colorWithRed:arc4random()%256 / 255.0 green:arc4random()%256 / 255.0 blue:arc4random()%256 / 255.0 alpha:1];
83
84 cell.selectionStyle = UITableViewCellSelectionStyleNone;// 取消cell点击效果
85 //cell.selectedBackgroundView
86
87 return cell;
88
89 }
90
91 #pragma mark UITableView 编辑-----
92
93 // 实现左按钮点击方法
94 - (void)leftBarButtonItemAction:(UIBarButtonItem *)sender
95 {
96 //左按钮进行插入
97 self.style = UITableViewCellEditingStyleInsert;
98 [self.rootView.tableView setEditing:!self.rootView.tableView.editing animated:YES];
99 }
100
101
102
103 // 实现右按钮方法
104 - (void)rightBarButtonItemAction:(UIBarButtonItem *)sender
105 {
106 // 第一步: 设置让tableView处于编辑状态
107 //self.rootView.tableView.editing = YES;
108 // if (self.rootView.tableView.editing) {
109 // self.rootView.tableView.editing = NO;
110 // } else {
111 // self.rootView.tableView.editing = YES;
112 // }
113 // 右按钮进行删除
114 self.style = UITableViewCellEditingStyleDelete;
115 // 优化写法
116 [self.rootView.tableView setEditing:!self.rootView.tableView.editing animated:YES];
117
118
119 }
120 // 第二步: 指定哪些cell可以被编辑 (默认所有cell都可以编辑)
121 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
122 {
123 if (indexPath.section == 0) {
124 return YES;
125 }
126 return NO;
127 }
128
129 // 第三步: 设置编辑样式 (默认编辑样式为删除样式)
130 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
131 {
132 return self.style;
133
134 }
135
136 // 第四步: 完成编辑, 提交编辑状态
137 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
138 {
139
140 // 判断编辑样式
141 if (editingStyle == UITableViewCellEditingStyleDelete) {
142 // 1.删除数据
143 [self.allDataArray[indexPath.section] removeObjectAtIndex:indexPath.row];
144 // 2.更新UI
145 // 更新一行
146 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
147 // 全部更新
148 //[tableView reloadData];
149 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
150 // 1. 添加数据到数组中
151 [self.allDataArray[indexPath.section] insertObject:@"你是不是啥" atIndex:indexPath.row + 1];
152
153 // 2. 更行UI
154 // [tableView reloadData];
155 //更新一行
156 /**
157 * 创建新一行的NSIndexPath对象
158 */
159
160 NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
161
162 [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationBottom];
163 }
164 }
165
166 // 返回每一行的高度
167 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
168 {
169 return 80;
170 }
171
172 // 点击cell取消动画
173 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
174 {
175 [tableView deselectRowAtIndexPath:indexPath animated:YES];
176 }