iOS7 Deleting Cells and Sections from TableViews

####iOS7 Programming Cookbook 第四章 Deleting Cells and Sections from 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
176
177
178
179
180
181
182
183
184
185
186
187
188
static NSString *CellIdentifier = @"NumbersCellIdentifier";

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
//tableview
@property (nonatomic, strong) UITableView *tableViewNumbers;
//字典
@property (nonatomic, strong) NSMutableDictionary *dictionaryOfNumbers;
//barbuttonitem
@property (nonatomic, strong) UIBarButtonItem *barButtonAction;
@end

static NSString *SectionOddNumbers = @"Odd Numbers";
static NSString *SectionEvenNumbers = @"Even Numbers";

@implementation ViewController

//2 sections
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
/**
* self.dictionaryOfNumbers.allKeys = (
"Odd Numbers",
"Even Numbers"
)
*/

return self.dictionaryOfNumbers.allKeys.count;
}

//4 rows
- (NSInteger) tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
//section = 1;Even Numbers;
NSString *sectionNameInDictionary = self.dictionaryOfNumbers.allKeys[section];
/**
* (
0,
2,
4,
6
)
*/

NSArray *sectionArray = self.dictionaryOfNumbers[sectionNameInDictionary];
return sectionArray.count;

}

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

UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
//字典中对应的key
NSString *sectionNameInDictionary =
self.dictionaryOfNumbers.allKeys[indexPath.section];
//字典对应的Array
NSArray *sectionArray = self.dictionaryOfNumbers[sectionNameInDictionary];
//array中的每一项
NSNumber *number = sectionArray[indexPath.row];

//unsignedIntegerValue 返回NSUInteger
cell.textLabel.text = [NSString stringWithFormat:@"%lu",
(unsigned long)[number unsignedIntegerValue]];

return cell;

}

//section title
- (NSString *) tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section{
return self.dictionaryOfNumbers.allKeys[section];

}

- (NSMutableDictionary *) dictionaryOfNumbers{
if (_dictionaryOfNumbers == nil){
//可变数组EvenNumbers
NSMutableArray *arrayOfEvenNumbers =
[[NSMutableArray alloc] initWithArray:@[@0,@2,@4,@6,]];

//可变数组OddNumbers
NSMutableArray *arrayOfOddNumbers =
[[NSMutableArray alloc] initWithArray:@[@1,@3,@5,@7,]];

//初始化NSMutableDictionary为 key:value
_dictionaryOfNumbers =[[NSMutableDictionary alloc] initWithDictionary:@{SectionEvenNumbers : arrayOfEvenNumbers ,SectionOddNumbers : arrayOfOddNumbers}];

}
return _dictionaryOfNumbers;
}

/* 事件*/
- (void) deleteNumbersGreaterThan2:(id)paramSender{

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

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

/* 删除源和索引路径 */
__block NSUInteger keyIndex = 0;
//适用于一个给定的块对象字典的条目。
[self.dictionaryOfNumbers enumerateKeysAndObjectsUsingBlock:
^(NSString *key, NSMutableArray *object, BOOL *stop) {
//使用块枚举对象
[object enumerateObjectsUsingBlock:
^(NSNumber *number, NSUInteger numberIndex, BOOL *stop) {
//Returns the receiver’s value as an NSUInteger.
//大于2的添加为删除对象
if ([number unsignedIntegerValue] > 2){

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:numberIndex
inSection:keyIndex];

[arrayOfIndexPathsToDelete addObject:indexPath];
[arrayOfNumberObjectsToDelete addObject:number];
}

}];

keyIndex++;
}];

/* Step 2: 从数据源中删除对象 */
if ([arrayOfNumberObjectsToDelete count] > 0){
//Odd Numbers数组
NSMutableArray *arrayOfOddNumbers =
self.dictionaryOfNumbers[SectionOddNumbers];
//Even Numbers数组
NSMutableArray *arrayOfEvenNumbers =
self.dictionaryOfNumbers[SectionEvenNumbers];


[arrayOfNumberObjectsToDelete enumerateObjectsUsingBlock:
^(NSNumber *numberToDelete, NSUInteger idx, BOOL *stop) {
//NSNotFound:一个值,表明一项请求无法找到或不存在。
//OddNumbers
if ([arrayOfOddNumbers indexOfObject:numberToDelete]!= NSNotFound){
//移除
[arrayOfOddNumbers removeObject:numberToDelete];
}
//EvenNumbers
if ([arrayOfEvenNumbers indexOfObject:numberToDelete]!= NSNotFound){
[arrayOfEvenNumbers removeObject:numberToDelete];
}
}];
}

/* Step 3: 删除对应的对象 */
[self.tableViewNumbers deleteRowsAtIndexPaths:arrayOfIndexPathsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];

//RightBarButtonItem = nil ,动画yes
[self.navigationItem setRightBarButtonItem:nil animated:YES];

}

- (void)viewDidLoad
{
[super viewDidLoad];
//UIBarButtonItem初始化,并添加deleteNumbersGreaterThan2事件
self.barButtonAction = [[UIBarButtonItem alloc] initWithTitle:@"Delete Numbers > 2" style:UIBarButtonItemStylePlain target:self action:@selector(deleteNumbersGreaterThan2:)];

//设置right navigationItem
[self.navigationItem setRightBarButtonItem:self.barButtonAction
animated:NO];
//初始化tableview,style为group类型
self.tableViewNumbers = [[UITableView alloc]
initWithFrame:self.view.frame
style:UITableViewStyleGrouped];
//注册cell类
[self.tableViewNumbers registerClass:[UITableViewCell class]
forCellReuseIdentifier:CellIdentifier];

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

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

}

@end

Reference

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