iOS7 Moving Cells and Sections in TableViews

####iOS7 Programming Cookbook 第四章 Moving Cells and Sections in TableViews

#####ViewController.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175


static NSString *CellIdentifier = @"CellIdentifier";

@interface ViewController ()
//tableview
@property (nonatomic, strong) UITableView *myTableView;
//可变数组
@property (nonatomic, strong) NSMutableArray *arrayOfSections;
@end

@implementation ViewController
//生成各个数组的方法
- (NSMutableArray *)newSectionWithIndex:(NSUInteger)paramIndex
cellCount:(NSUInteger)paramCellCount{

NSMutableArray *result = [[NSMutableArray alloc] init];

NSUInteger counter = 0;
for (counter = 0;counter < paramCellCount; counter++){
[result addObject:[[NSString alloc] initWithFormat:@"Section %lu Cell %lu",(unsigned long)paramIndex,(unsigned long)counter+1]];

}

return result;

}

//初始化可变数组 arrayOfSections
- (NSMutableArray *) arrayOfSections{
//如果为空
if (_arrayOfSections == nil){
/**
* (
Section 1 Cell 1,
Section 1 Cell 2,
Section 1 Cell 3
)
*/

//初始化3组可变数组
NSMutableArray *section1 = [self newSectionWithIndex:1 cellCount:3];
NSMutableArray *section2 = [self newSectionWithIndex:2 cellCount:3];
NSMutableArray *section3 = [self newSectionWithIndex:3 cellCount:3];
//三组可变数组添加到一个可变数组
_arrayOfSections = [[NSMutableArray alloc] initWithArray:@[section1,section2,section3]];
}
return _arrayOfSections;
}

//section数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return self.arrayOfSections.count;

}

//rows
- (NSInteger) tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{

NSMutableArray *sectionArray = self.arrayOfSections[section];
return sectionArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = nil;

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];

NSMutableArray *sectionArray = self.arrayOfSections[indexPath.section];

cell.textLabel.text = sectionArray[indexPath.row];

return cell;

}

- (void)viewDidLoad{
[super viewDidLoad];

//初始化tableview,style为group类型
self.myTableView =
[[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStyleGrouped];

//注册cell类
[self.myTableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:CellIdentifier];

//自动调整布局
self.myTableView.autoresizingMask =
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;

//datasource,delegate指向ViewController
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
//添加到view
[self.view addSubview:self.myTableView];

//延迟秒
double delayInSeconds = 2.0;
//创建一个dispatch_time_t相对于默认时钟或修改现有dispatch_time_t。
//NSEC_PER_SEC 纳秒每秒
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
//主线程操作
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self moveCell2InSection1ToCell1InSection2];
});

}

/**
* 实现方法
*/

- (void) moveCell2InSection1ToCell1InSection2{
//arrayOfSections数组第一个数组
NSMutableArray *section1 = self.arrayOfSections[0];
//arrayOfSections数组第二个数组
NSMutableArray *section2 = self.arrayOfSections[1];
//第一个数组的,第二个string
NSString *cell2InSection1 = section1[1];
//移走
[section1 removeObject:cell2InSection1];
//插入到第二个数组的第一个位置
[section2 insertObject:cell2InSection1
atIndex:0];
//indexpath
NSIndexPath *sourceIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
NSIndexPath *destinationIndexPath = [NSIndexPath indexPathForRow:0 inSection:1];
//移走操作
[self.myTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];

}

/**
* 其他
*/


- (void) moveCell1InSection1ToCell2InSection1{
//arrayOfSections数组的第一个数组
NSMutableArray *section1 = self.arrayOfSections[0];
//第一个数组的第一个string
NSString *cell1InSection1 = section1[0];
//移走
[section1 removeObject:cell1InSection1];

[section1 insertObject:cell1InSection1
atIndex:1];

NSIndexPath *sourceIndexPath = [NSIndexPath indexPathForRow:0
inSection:0];
NSIndexPath *destinationIndexPath = [NSIndexPath indexPathForRow:1
inSection:0];

[self.myTableView moveRowAtIndexPath:sourceIndexPath
toIndexPath:destinationIndexPath];

}

- (void) moveSection1ToSection3{

NSMutableArray *section1 = self.arrayOfSections[0];
[self.arrayOfSections removeObject:section1];
[self.arrayOfSections addObject:section1];

[self.myTableView moveSection:0
toSection:2];

}

@end

Reference

坚持原创技术分享,您的支持将鼓励我继续创作!
0%