詳解iOS App中UIPickerView滾動選擇欄的添加方法
1.UIPickerView的寬度和高度是固定的,縱向是320216,橫向是568162
2.屬性:
@property(nonatomic,readonly)NSInteger numberOfComponents; // 選擇框的行數(shù)
@property(nonatomic,assign)idUIPickerViewDataSource> dataSource; (類似于UITableView)
@property(nonatomic,assign)idUIPickerViewDelegate>delegate; (類似于UITableView)
(BOOL)showsSelectionIndicator// 是否顯示選擇指示器 ,即是一個藍色的條
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
// 指定Delegate
pickerView.delegate=self;
// 顯示選中框
pickerView.showsSelectionIndicator=YES;
[self.view addSubview:pickerView];
以上可以在視圖顯示一個選取器,但是內(nèi)容空白,pickerView.showsSelectionIndicator=YES;是這只當(dāng)前選取器所選中的內(nèi)容:
選取器上顯示數(shù)據(jù),必須依賴兩個協(xié)議,UIPickerViewDelegate和UIPickerViewDataSource,把他們添加到ViewController.h文件中
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
{
UIPickerView *pickerView;
NSArray *pickerData;
}
@end
3.然后在.m文件的ViewDidLoad中初始化界面
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
// 指定Delegate
pickerView.delegate=self;
// 顯示選中框
pickerView.showsSelectionIndicator=YES;
[self.view addSubview:pickerView];
NSArray *dataArray = [[NSArray alloc]initWithObjects:@"許嵩",@"周杰倫",@"梁靜茹",@"許飛",@"鳳凰傳奇",@"阿杜",@"方大同",@"林俊杰",@"胡夏",@"邱永傳", nil];
pickerData=dataArray;
// 添加按鈕
CGRect frame = CGRectMake(120, 250, 80, 40);
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
selectButton.frame=frame;
[selectButton setTitle:@"SELECT" forState:UIControlStateNormal];
[selectButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:selectButton];
}
4.實現(xiàn)UIPickerView的代理方法,將數(shù)據(jù)顯示在選取器上所需要幾個方法
#pragma mark -
#pragma mark Picker Date Source Methods
//返回顯示的列數(shù)
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
//返回當(dāng)前列顯示的行數(shù)
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [pickerData count];
}
#pragma mark Picker Delegate Methods
//返回當(dāng)前行的內(nèi)容,此處是將數(shù)組中數(shù)值添加到滾動的那個顯示欄上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [pickerData objectAtIndex:row];
}
前兩個是數(shù)據(jù)源的代理方法,一個是返回列,有幾個選取器就返回幾,第二個是設(shè)置選取器有多少行,因為就這一個選取器,所以直接返回行數(shù),即數(shù)組元素個數(shù)多少;第三個代理方法是將數(shù)組元素添加到了選取器上面顯示;
說一下兩個協(xié)議實例方法
UIPickerViewDelegate中的實例方法
// 當(dāng)用戶選擇某個row時
- (void) pickerView: (UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger)component
// 當(dāng)其在繪制row內(nèi)容,需要row的高度時
(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent: (NSInteger) component
// 返回指定component.row顯示的文本
(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component
// 當(dāng)picker view需要給指定的component.row指定view時,調(diào)用此函數(shù).返回值為用作row內(nèi)容的view
(UIView *)pickerView: (UIPickerView *)pickerView view ForRow:(NSInteger) row forComponent:(NSInteger) component reusingView:(UIView *) view
// row的寬度
(CGFloat)pickerView: (UIPickerView *)pickerView widthForComponent:(NSInteger) component
UIPickerViewDataSource中的實例方法
按照官方文檔的說法,UIPickerViewDataSource這個協(xié)議僅有的功能就是提供picker view中component的個數(shù)和各個component中的row的個數(shù),雖然名為datasource,但是它工作于MVC的C中
本協(xié)議僅有兩個實例方法,均需要實現(xiàn):
// 返回列數(shù)
(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
// 返回每一列對應(yīng)的行數(shù)
(NSInteger) pickerView:(UIPickerView *) pickerView numberOfRowsInComponent:(NSInteger) component
5.關(guān)于按鈕響應(yīng)事件,關(guān)于按鈕的形成和添加響應(yīng)事件不再提,前面都有,
(void) buttonPressed:(id)sender
{
NSInteger row =[pickerView selectedRowInComponent:0];
NSString *selected = [pickerData objectAtIndex:row];
NSString *message = [[NSString alloc] initWithFormat:@"你選擇的是:%@",selected];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:message
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
@UIPickerView還有其他實例方法
// 獲取指定列的行數(shù)
- (NSInteger) numberOfRowsInComponent:(NSInteger)component
// 刷新所有的列
(void) reloadAllComponents
// 刷新指定的列
(void) reloadComponent: (NSInteger) component
(CGSize) rowSizeForComponent: (NSInteger) component
// 獲取某列選擇的行數(shù)
(NSInteger) selectedRowInComponent: (NSInteger) component
// 選擇一行
(void) selectRow: (NSInteger)row inComponent: (NSInteger)component animated: (BOOL)animated
(UIView *) viewForRow: (NSInteger)row forComponent: (NSInteger)component
PS:多個component對應(yīng)不同title的方法
有時候我們需要有多個component的UIPickerView并且對應(yīng)不同的內(nèi)容,比如地區(qū)的選擇,需要有省份和城市兩個選項,選擇不同的省份,城市要相應(yīng)發(fā)生變化。
下面假設(shè)component數(shù)量為2。
使用指定title的函數(shù),根據(jù)[pickerView selectedRowInComponent:0]的不同來指定第二個component的title
- (NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
}
但此時,會發(fā)現(xiàn)切換省份后,城市一欄沒有辦法及時刷新。
我們還要指定刷新事件。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[pickerView reloadComponent:1];
}
相關(guān)文章
iOS將時間NSDate轉(zhuǎn)化為毫秒時間戳的方法示例
這篇文章主要給大家介紹了關(guān)于iOS將時間NSDate轉(zhuǎn)化為毫秒時間戳的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08iOS開發(fā)CGContextRef畫圖使用總結(jié)
這篇文章主要為大家詳細介紹了iOS開發(fā)CGContextRef畫圖使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04iOS中UIWebView網(wǎng)頁加載組件的基礎(chǔ)及使用技巧實例
UIWebView是開發(fā)中很常用的應(yīng)用內(nèi)調(diào)用網(wǎng)頁瀏覽的控件,這里整理了一些iOS中UIWebView網(wǎng)頁加載組件的基礎(chǔ)及使用技巧實例 ,需要的朋友可以參考下2016-06-06iOS開發(fā)實現(xiàn)UIImageView的分類
這篇文章主要為大家詳細介紹了iOS開發(fā)實現(xiàn)UIImageView的分類,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01Objective-C 經(jīng)典字典數(shù)組排序 - 省市區(qū)
本文主要介紹Objective-C 字典數(shù)組排序,這里整理相關(guān)資料及實現(xiàn)示例代碼,有興趣的小伙伴可以參考下2016-09-09iOS 仿微博客戶端紅包加載界面 XLDotLoading效果
這篇文章主要介紹了iOS 仿微博客戶端紅包加載界面 XLDotLoading,需要的朋友可以參考下2017-02-02