ios UITableView实现单击提示,滑动删除,多选行等操作

来源:互联网 发布:犀牛o2o源码 编辑:程序博客网 时间:2024/06/16 01:49

demo功能:ios UITableView实现单击提示,滑动删除,多选行等操作。iphone 6.1测试通过。

demo说明:DeleteMeController.m;CheckListController.m这些都是用UITableView实现的效果代码。

demo截屏:

   

ios UITableView实现单击提示,滑动删除,多选行等操作  ios UITableView实现单击提示,滑动删除,多选行等操作  ios UITableView实现单击提示,滑动删除,多选行等操作

demo主要代码:

1#import "DeleteMeController.h"   
2   
3@implementation DeleteMeController  
4@synthesize list;  
5-(IBAction)toggleEdit:(id)sender {  
6    [self.tableView setEditing:!self.tableView.editing animated:YES];     
7}  
8#pragma mark -   
9- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {  
10    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {  
11        // Initialization code   
12    }  
13    return self;  
14}  
15   
16- (void)viewDidLoad {  
17    NSString *path = [[NSBundle mainBundle] pathForResource:@"computers"ofType:@"plist"];  
18    NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];  
19    self.list = array;  
20       
21    UIBarButtonItem *editButton = [[[UIBarButtonItem alloc]  
22                                    initWithTitle:@"Delete" 
23                                    style:UIBarButtonItemStyleBordered  
24                                    target:self  
25                                    action:@selector(toggleEdit:)] autorelease];  
26    self.navigationItem.rightBarButtonItem = editButton;  
27       
28    [super viewDidLoad];  
29}  
30  
31  
32#pragma mark -  
33#pragma mark Table Data Source Methods   
34- (NSInteger)tableView:(UITableView *)tableView   
35 numberOfRowsInSection:(NSInteger)section {  
36    return [list count];  
37}  
38- (UITableViewCell *)tableView:(UITableView *)tableView   
39         cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
40       
41    static NSString *DeleteMeCellIdentifier = @"DeleteMeCellIdentifier";  
42       
43    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DeleteMeCellIdentifier];  
44    if (cell == nil) {  
45        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero   
46                                       reuseIdentifier:DeleteMeCellIdentifier] autorelease];  
47    }  
48    NSInteger row = [indexPath row];  
49    cell.text = [self.list objectAtIndex:row];  
50    return cell;  
51}  
52  
53#pragma mark -  
54#pragma mark Table Delegate Methods   
55- (void)tableView:(UITableView *)tableView   
56commitEditingStyle:(UITableViewCellEditingStyle)editingStyle   
57forRowAtIndexPath:(NSIndexPath *)indexPath {  
58       
59    NSUInteger row = [indexPath row];  
60    [self.list removeObjectAtIndex:row];  
61    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]   
62                     withRowAnimation:UITableViewRowAnimationFade];  
63}  
64   
65@end  
66 
67#import "DeleteMeController.h"
68 
69@implementation DeleteMeController
70@synthesize list;
71-(IBAction)toggleEdit:(id)sender {
72    [self.tableView setEditing:!self.tableView.editing animated:YES];   
73}
74#pragma mark -
75- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
76    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
77        // Initialization code
78    }
79    return self;
80}
81 
82- (void)viewDidLoad {
83    NSString *path = [[NSBundle mainBundle] pathForResource:@"computers"ofType:@"plist"];
84    NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
85    self.list = array;
86     
87    UIBarButtonItem *editButton = [[[UIBarButtonItem alloc]
88                                    initWithTitle:@"Delete"
89                                    style:UIBarButtonItemStyleBordered
90                                    target:self
91                                    action:@selector(toggleEdit:)] autorelease];
92    self.navigationItem.rightBarButtonItem = editButton;
93     
94    [super viewDidLoad];
95}
96 
97 
98#pragma mark -
99#pragma mark Table Data Source Methods
100- (NSInteger)tableView:(UITableView *)tableView 
101 numberOfRowsInSection:(NSInteger)section {
102    return [list count];
103}
104- (UITableViewCell *)tableView:(UITableView *)tableView 
105         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
106     
107    static NSString *DeleteMeCellIdentifier = @"DeleteMeCellIdentifier";
108     
109    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DeleteMeCellIdentifier];
110    if (cell == nil) {
111        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
112                                       reuseIdentifier:DeleteMeCellIdentifier] autorelease];
113    }
114    NSInteger row = [indexPath row];
115    cell.text = [self.list objectAtIndex:row];
116    return cell;
117}
118 
119#pragma mark -
120#pragma mark Table Delegate Methods
121- (void)tableView:(UITableView *)tableView 
122commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
123forRowAtIndexPath:(NSIndexPath *)indexPath {
124     
125    NSUInteger row = [indexPath row];
126    [self.list removeObjectAtIndex:row];
127    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
128                     withRowAnimation:UITableViewRowAnimationFade];
129}
130 
131@end
0 0
原创粉丝点击