Fork me on GitHub

基于tabBar类似mail多选删除的纠结实现

本来tableview多选删除是天经地义的事儿。在tabbar中想使用tableview的多选删除也没啥不对。纠结就纠结在想在tabbar中使用tableview多选删除的时候要实现类似系统自带的mail多选删除的效果。好吧,既然产品是这么设计的,合理不合理是一回事儿,能不能实现却是我的事儿,抱着“只要能实现,我就能实现的信念”,纠结了七八个小时,终于把这个不起眼的小效果搞定了。

开始界面

点了编辑以后。

选择要删除的行

点删除以后的效果

点清空以后的效果

点确定以后的效果

核心代码除了实现tabbar多选删除效果的以外,增加一个隐藏tabbar,一个在toolbar添加两个删除按钮及相关的处理方法。

 
 1 - (void) hideTabBar:(BOOL) hidden{ 
2 [UIView beginAnimations:nil context:NULL];
3 [UIView setAnimationDuration:0];
4 for(UIView *view in self.tabBarController.view.subviews)
5 {
6 if([view isKindOfClass:[UITabBar class]])
7 {
8 if (hidden) {
9 [view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
10 } else {
11 [view setFrame:CGRectMake(view.frame.origin.x, 480-49, view.frame.size.width, view.frame.size.height)];
12 }
13 }
14 else
15 {
16 if (hidden) {
17 [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
18 } else {
19 [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480-49)];
20 }
21 }
22 }
23 [UIView commitAnimations];
24 }
25 - (void)buttonClicked:(id)sender{
26 switch ([sender tag]) {
27 case 1:
28 //NSLog(@"要删除喽");
29 [dataArray removeObjectsInArray:[deleteDic allKeys]];
30
31 [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:[deleteDic allValues]] withRowAnimation:UITableVi              ewRowAnimationFade];
32 [deleteDic removeAllObjects];
33 break;
34 case 2:
35 //NSLog(@"要清空喽");
36 ; NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
37
38 int end = [dataArray count];
39 for (int i = 0; i<end; i++) {
40 [tmpArray addObject:[NSIndexPath indexPathForRow:i inSection:0]];
41 }
42
43 [dataArray removeAllObjects];
44 [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:tmpArray] withRowAnimation:UITableViewRowAnimationFade];
45 [tmpArray release];
46 break;
47 default:
48 break;
49 }
50 }
51 - (void)clearAllHistory{
52 if (self.navigationItem.rightBarButtonItem.title == @"编辑") {
53
54 //隐藏tabbar
55 [self hideTabBar:YES];
56 //显示toolbar
57 self.navigationController.toolbar.barStyle = UIBarStyleBlack;
58 self.navigationController.toolbarHidden = NO;
59
60 if (selectBtn == nil) {
61 UIButton *myBtn1 = [UIButton buttonWithType:UIBarButtonItemStylePlain];
62 selectBtn = myBtn1;
63 myBtn1.frame = CGRectMake(0.0f, 0.0f, 80.0f, 30.0f);
64 [myBtn1 setTitle:@"删除(0)" forState:UIControlStateNormal];
65 [myBtn1 setTag:1];
66 [myBtn1 setBackgroundImage:[UIImage imageNamed:@"bbg.png"] forState:UIControlStateNormal];
67 [myBtn1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
68 deleteBtn =[[UIBarButtonItem alloc] initWithCustomView:myBtn1];
69
70 UIButton *myBtn2 = [UIButton buttonWithType:UIBarButtonItemStylePlain];
71 myBtn2.frame = CGRectMake(0.0f, 0.0f, 80.0f, 30.0f);
72 [myBtn2 setTitle:@"清空" forState:UIControlStateNormal];
73 [myBtn2 setTag:2];
74 [myBtn2 setBackgroundImage:[UIImage imageNamed:@"bbg.png"] forState:UIControlStateNormal];
75 [myBtn2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
76 clearBtn = [[UIBarButtonItem alloc] initWithCustomView:myBtn2 ];
77
78 spaceBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil ac                tion:nil];
79 }
80
81 [self.navigationController.toolbar setItems:[NSArray arrayWithObjects:spaceBtn,spaceBtn,deleteBtn,clearBtn,spaceBtn,                spaceBtn,nil] animated:YES];
82 self.navigationItem.rightBarButtonItem.title = @"确定";
83 [self.tableView setEditing:YES animated:YES];
84 }
85 else {
86 self.navigationItem.rightBarButtonItem.title = @"编辑";
87 self.navigationController.toolbarHidden = YES;
88 [self hideTabBar:NO];
89 [deleteDic removeAllObjects];
90 [self.tableView setEditing:NO animated:YES];
91 }
92 }
整个效果实现起来的难点有三个:
1.tabbar的隐藏。
作为要控制器,它竟然不允许自己被隐藏。弄来弄去,最后隐藏toolbar的方法用的是cocochina的laigb提供的方法。http://www.cocoachina.com/bbs/read.php?tid=25042&page=3#206318.不过laigb用的y的值为320.应该是iphone4以前的设备要用这个值吧。
2.两个删除按钮的相对居中
toolbar默认添加的按钮是从左相右排列的。添加两个的话,就默认跑到最左边了。所以这个地方要使用占位符。
3.修改标签:"删除(0)"
这个地方纠结的地方有两处,
1)保存这个按钮的引用,方便修改其值;
2)修改它的值的时候发现,必需选用"删除(0)"占位,不然后面再修改成类似"删除(1)"的时候,显示不全
转载自:http://rainbird.blog.51cto.com/211214/645772

posted on 2012-02-05 18:50  pengyingh  阅读(324)  评论(0编辑  收藏  举报

导航