iOS7 Enabling Swipe Deletion of TableViewCells

####iOS7 Programming Cookbook 第四章 Enabling Swipe Deletion of Table View Cells

#####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

static NSString *MyCellIdentifier = @"SimpleCells";

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
//tableview
@property (nonatomic, strong) UITableView *myTableView;
//可变数组
@property (nonatomic, strong) NSMutableArray *allRows;
@end

@implementation ViewController
//要求连续的编辑风格在一个特定的位置在表视图中
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

return UITableViewCellEditingStyleDelete;

}

//设置视图控制器是否显示了一个可编辑
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[self.myTableView setEditing:editing animated:animated];
}

//提交编辑风格
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete){
//从源删除对象
[self.allRows removeObjectAtIndex:indexPath.row];

//然后从视图中删除相关cell
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}

//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:MyCellIdentifier forIndexPath:indexPath];

cell.textLabel.text = self.allRows[indexPath.row];

return cell;

}

- (NSMutableArray *) allRows{
if (_allRows == nil){
//总数10
const NSUInteger numberOfItems = 10;
//初始化_allRows
_allRows = [[NSMutableArray alloc] initWithCapacity:numberOfItems];
//0到9
for (NSUInteger counter = 0;counter < numberOfItems; counter++){
//添加string到rows
[_allRows addObject:[[NSString alloc] initWithFormat:@"Cell Item At Index of %lu",
(unsigned long)counter]];
}
}
return _allRows;
}

//10 rows
- (NSInteger) tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
return [self.allRows count];

}

- (void)viewDidLoad{
[super viewDidLoad];

//使用布局,显示扩展边缘
[self.navigationItem setLeftBarButtonItem:self.editButtonItem animated:NO];
//初始化tableview,style为plain类型
self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

//注册cell类
[self.myTableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:MyCellIdentifier];
//datasource,delegate指向ViewController
self.myTableView.dataSource = self;
self.myTableView.delegate = self;
//自动调整布局
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//添加到view
[self.view addSubview:self.myTableView];

}

@end

Reference

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