標籤:
其實UIPickerView和UITableview的實現方法中,大致是相同的,就連作用都是一樣的,只是運用在了不同的效果上而已,那麼下面就記錄幾個方法就可以實現UIPickerView的效果:
1、設定UIPickerView的列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
2、設定UIPickerView 的行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
3、設定UIPickerView的行高
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
4、設定UIPickerView的文本資訊
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
5、UIPickerView的點擊事件
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
通過以上的5個步驟,就能實現一個滾動的效果。但是呢,這裡有一個問題,就是不能像UITableView中的Cell方法一樣去設定[MoreCell.textLabel setFont:[UIFont systemFontOfSize:15.0f]];的樣式或者字型大小,那麼這裡就涉及到了UIPickerView中的另外一個實現方法:
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ UILabel *lalTitle=(UILabel *)view; if (!lalTitle) { lalTitle=[[UILabel alloc] init]; lalTitle.minimumScaleFactor=8;//設定最小字型,與minimumFontSize相同,minimumFontSize在IOS 6後不能使用。 lalTitle.adjustsFontSizeToFitWidth=YES;//設定字型大小是否適應lalbel寬度 lalTitle.textAlignment=NSTextAlignmentCenter;//文字置中顯示 [lalTitle setTextColor:[UIColor blackColor]]; [lalTitle setFont:[UIFont systemFontOfSize:17.0f]]; } lalTitle.text=[self pickerView:pickerView titleForRow:row forComponent:component]; return lalTitle; }
到此,UIPickerView的簡單實用就實現了!!!!
iOS UIPickerView的簡單實用