1 //
2 // HMViewController.m
3 // 06-表格的修改
4 //
5 // Created by apple on 14-8-19.
6 // Copyright (c) 2014年 itcast. All rights reserved.
7 //
8
9 #import "HMViewController.h"
10
11 @interface HMViewController () <UITableViewDataSource, UITableViewDelegate>
12 /** 数据列表 */
13 @property (nonatomic, strong) NSMutableArray *dataList;
14 @property (nonatomic, strong) UITableView *tableView;
15 @end
16
17 @implementation HMViewController
18
19 - (UITableView *)tableView
20 {
21 if (_tableView == nil) {
22 _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
23
24 _tableView.dataSource = self;
25 _tableView.delegate = self;
26
27 [self.view addSubview:_tableView];
28 }
29 return _tableView;
30 }
31
32 - (NSMutableArray *)dataList
33 {
34 if (_dataList == nil) {
35 _dataList = [NSMutableArray arrayWithObjects:@"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwuwangwuwangwuwangwuwangwu", nil];
36 }
37 return _dataList;
38 }
39
40 - (void)viewDidLoad
41 {
42 [super viewDidLoad];
43
44 [self tableView];
45
46 // 开始编辑,一旦editing == YES就默认开启删除模式
47 self.tableView.editing = YES;
48 }
49
50 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
51 {
52 return self.dataList.count;
53 }
54
55 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
56 {
57 static NSString *ID = @"Cell";
58
59 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
60
61 if (cell == nil) {
62 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
63 }
64
65 // 设置表格
66 cell.textLabel.text = self.dataList[indexPath.row];
67
68 return cell;
69 }
70
71 // 只要实现了此方法,就能够支持手势拖拽删除了,删除需要自己干!
72 /**
73 UITableViewCellEditingStyleNone,
74 UITableViewCellEditingStyleDelete, 删除
75 UITableViewCellEditingStyleInsert 添加
76 */
77 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
78 {
79 if (editingStyle == UITableViewCellEditingStyleDelete) {
80 NSLog(@"要删除");
81
82 // MVC => 数据是保存在模型中
83 // 1. 删除self.dataList中indexPath对应的数据
84 [self.dataList removeObjectAtIndex:indexPath.row];
85 NSLog(@"%@", self.dataList);
86
87 // 2. 刷新表格(重新加载数据)
88 // 重新加载所有数据
89 // [self.tableView reloadData];
90 // deleteRowsAtIndexPaths让表格控件动画删除指定的行
91 [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
92 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
93 NSLog(@"要添加数据");
94
95 // 1. 向数组添加数据
96 [self.dataList insertObject:@"王小二" atIndex:indexPath.row + 1];
97 // 2. 刷新表格
98 // [self.tableView reloadData];
99 // insertRowsAtIndexPaths让表格控件动画在指定indexPath添加指定行
100 // 新建一个indexPath
101 NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
102
103 [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
104 }
105 }
106
107 // 只要实现此方法,就可以显示拖动控件
108 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
109 {
110 // 界面数据UITableView已经完成了
111 // 调整数据即可
112 // [self.dataList exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
113 // 1. 将源从数组中取出
114 id source = self.dataList[sourceIndexPath.row];
115 // 2. 将源从数组中删除
116 [self.dataList removeObjectAtIndex:sourceIndexPath.row];
117 NSLog(@"%@", self.dataList);
118
119 // 3. 将源插入到数组中的目标位置
120 [self.dataList insertObject:source atIndex:destinationIndexPath.row];
121
122 NSLog(@"%@", self.dataList);
123 }
124
125 #pragma mark - 代理方法
126 // 返回编辑样式,如果没有实现此方法,默认都是删除
127 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
128 {
129 // if (indexPath.row % 2) {
130 // return UITableViewCellEditingStyleInsert;
131 // } else {
132 // return UITableViewCellEditingStyleDelete;
133 // }
134 return UITableViewCellEditingStyleInsert;
135 }
136
137 @end