Add UIPickerView & a Button in Action sheet - How? or How to add a UITableView to a UIPickerView on the same view

我想实现的效果:

在 uitableview的中的某一行选中后,弹出pickerview,进行选择,当点击其他行时退出。

注意 uitableview的某一行 不一定是最后一行,所以不能简单的 将uitableview加到mainview后,将pickerview也加到mianview,只要控制位置。

我的想法:

因为有可能在中间一行,所以我想将pickerview 加到uitableview的某一行,并且要设置这个一行的高度,并且根据是否需要显示pickerview动态显示这一行。

 

后来搜到以下资料,好像可以实现类似效果,所以就采用以下替换方法。

http://stackoverflow.com/questions/1262574/add-uipickerview-a-button-in-action-sheet-how

 

 

http://stackoverflow.com/questions/5737773/how-to-add-a-uitableview-to-a-uipickerview-on-the-same-view

 

alt text

 

 


其中,UIToolBar的对齐方式是左对齐的,如果想要右对齐,或居中对齐,可以采用填充的方式,具体如下  见其中的flexspace
pickerDateToolbar
= [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerDateToolbar
.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];

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

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];

UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)];
[barItems addObject:doneBtn];

[pickerDateToolbar setItems:barItems animated:YES];


1.//实现如下,有个问题,设置uiactionsheet的高度总是不对。好像你怎么设 他都没反应,或者变化很大。算了,不适合我这里使用,放弃。
                wordThinkActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil] ;
                
                
                //wordThinkPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 40.0/*toolbar*/, 320, 60)];
                wordThinkPickerView = [[UIPickerView alloc] init];

                wordThinkPickerView.showsSelectionIndicator = YES;	// note this is default to NO
                // this view controller is the data source and delegate
                wordThinkPickerView.delegate = self;
                wordThinkPickerView.dataSource = self;
                
                
                
                UIToolbar*  pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
                pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
                [pickerDateToolbar sizeToFit];
                
                NSMutableArray *barItems = [[NSMutableArray alloc] init];
                
                //free space
                UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
                [barItems addObject:flexibleSpace];
                [flexibleSpace release] ;
                //done button
                UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(wordThinkPickerDoneClick)];
                [barItems addObject:doneBtn];
                [doneBtn release] ;
                
                [pickerDateToolbar setItems:barItems animated:YES];
                
                [wordThinkActionSheet addSubview:pickerDateToolbar];
                [wordThinkActionSheet addSubview:wordThinkPickerView];
                [wordThinkActionSheet showInView:self.view];
                //[wordThinkActionSheet showFromRect:CGRectMake(0,300,320, 180) inView:self.view animated:YES];
                [wordThinkActionSheet setBounds:CGRectMake(0,100,320, 300)];
                
                 [wordThinkPickerView setFrame:CGRectMake(0.0, 40.0/*toolbar*/, 320, 260)];
                [barItems release] ;
                [pickerDateToolbar release] ;

2.自己实现uipickerview从低部进入的动画效果。并且需要实现点击背景 消除uipickerview。
动画

You will avoid the display problem and get a much better looking result if you simply put the UIDatePicker in a new view and implement the slide-up-from-the-bottom behavior yourself. It's not that hard.

  1. Create a UIView containing a date picker and a toolbar with a Done button on it. (In code or use Interface Builder, doesn't matter.)
  2. Add the popup view as a subview of your main view.
  3. Set the popup view's frame so that it is off the bottom of the screen: frame.origin.y = CGRectGetMaxY(mainView.bounds)
  4. Call [UIView beginAnimations:nil context:nil]
  5. Set the frame to where it should be: frame.origin.y -= CGRectGetHeight(popupView.bounds)
  6. Call [UIView commitAnimations]

(Note that the frame setting stuff is pseudocode.)

That's it. Do it in reverse when you need to dismiss the view. I think it will be worth the trouble; sticking a date picker in a UIActionSheet looks incredibly tacky.

 

消除uipickerview
主要思想,给你的tableview 加 一个透明的UIVIew的派生类覆盖在talbeview上 然后在这个UIview的 touchBegan里写一个postNotification ,然后在原来的那个类处理 notification 事件,消除uipickerview即可。
NSnotification 使用见
http://stackoverflow.com/questions/2191594/how-to-send-and-receive-message-through-nsnotificationcenter-in-objective-c
uiview派生类实现

#import <UIKit/UIKit.h>

@interface BackgroundTapView : UIView{
}
@end
#import "BackgroundTapView.h" @implementation BackgroundTapView - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[NSNotificationCenter defaultCenter]postNotificationName:@"BackgroundTapNotification" object:self ] ; [super touchesBegan:touches withEvent:event]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesMoved:touches withEvent:event]; } - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { [super touchesEnded:touches withEvent:event]; } - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event { [super touchesCancelled:touches withEvent:event]; } @end

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值