Article From:https://www.cnblogs.com/soulDn/p/9967396.html

Selectors, I think everyone is familiar with, when users need to select a fixed value within certain range values, we will use the way of selectors. The selector can intuitively prompt the user to choose the range of values, uniform information filling format, but also facilitate the user to quickly select, such as for gender, normal.In this case, there are only two cases, male and female. When a selector is used to select the user, it can avoid the wrong data input and make it more convenient for the user to fill in. For example, if you need to get the user’s birthday information, you can use the selector to unify the format of the birthday, if you let the user enter it by himself.There may be a variety of birthday information formats, which is not conducive to data storage, but if the selector is used, the user can find the corresponding date for selection.

In iOS, there is a special selector class UIPickerView. When we enter the header file of UIPickerView, we can find that UIPickerView directly inherits UIView. Its event processing is done by proxy method, so we create UIPic.When you sign kerView, remember to sign its proxy UIPickerViewDataSource and UIPickerViewDelegate. In fact, it’s a bit like UITbleView, but UIPickerView has an additional column attributeWe’re all familiar with UITbleView, so we can learn to remember UIPickerView by comparing its properties and proxy methods.

I. The proxy method of UIPickerView
(1)UIPickerViewDataSourceThe corresponding proxy methods are (whose proxy methods must be implemented):

Returns the number of columns displayed
– (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

Returns the number of rows to be displayed in each column
– (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

(2)UIPickerViewDelegateThe corresponding proxy method (its proxy method is selectively implemented according to the requirements):

Returns the width of each column
– (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;

Returns the height of each column
– (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component ;

Returns the content to be displayed for the list item of the specified column in the UIPickerView control
– (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component ;

Returns the UIView as the display view of the specified row of the specified column in the UIPickerView control
– (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view ;

Select the specified row in the specified column
– (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component ;

II. UIPickerView and UIDatePicker.
UIDatePickerIt is a time and date selector packaged by the system. It is inherited from UIControl. UIDatePicker has some limitations because it has only four display styles:

UIDatePickerModeTime,
UIDatePickerModeDate,
UIDatePickerModeDateAndTime,
UIDatePickerModeCountDownTimer,
1
2
3
4
If the requirements and the four styles do not match, then we can not use UIDatePicker. For example, when only the year and month information is displayed, it is obvious that UIDatePicker can not meet our needs, then we can only use UIPickerVie.W comes from customizing the selector you want.

Custom Use of UIPickerView
(1)Create a WXZBasePickView whose base class inherits from UIView.
Our usual selector style is a view with a transparent background color. At the bottom is the content selector. There are confirm and cancel buttons, roughly as shown in the figure:

 

Download demo https://github.com/wxzwork/CustomPickView as needed

Leave a Reply

Your email address will not be published. Required fields are marked *