IOS development of PickerView text and random numbers
PickerView is used to display content to choose from (such as date selection, ordering, etc.).
There are three situations:
1. Each column is independently selected
2. The column on the right is affected by the column on the left
3. Include pictures
PickerView is similar to TableView, displaying data through data sources, and like TableView, let the controller be called its data source.
However, the PickerView's data source only provides the number of rows and columns, and the content can be set within the proxy method.
Set the number of rows and columns through two data source methods, and set the content through a proxy method. Note that the component represents which column:
The foods members here are a composite array, that is, there are multiple NSArrays in the NSArray. Each inner layer of NSArray contains a type of food, and different inner layer of NSArrays represent different types of food.
To get such a composite array, you can create it directly, for example:
@[@[...],@[...],@[...]];
Or read through plist
#pragma mark - PickerView data source method // returns the number of 'columns' to display. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return ; } // returns the # of rows in each component.. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ NSArray *foodArray = [component]; // component is a column return ; } #pragma mark - PickerView proxy method - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ return [component][row]; }
To listen for selection, you only need to implement another proxy method:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ NSString *name = [component][row]; switch (component) { case 0: = name; break; case 1: = name; break; case 2: = name; break; default: break; } }
One detail: Initialization of display data when there is no click:
Indirectly use the above selection method to initialize the data: row 0 is selected for each column. Since pickerView itself cannot be used, it is okay to pass nil.
for (int i = 0; i < ; i++) { [self pickerView:nil didSelectRow:0 inComponent:i]; }
Tip: A navigation toolbar is often used above the keyboard to switch to previous, next, etc.
Directly get the current selection: selectRowInComponent: Pass the column number to get which row is selected.
Random number:
arc4random() can generate 0 or positive integers. To generate random numbers of 0 ~ (x-1), arc4random() % x should be used.
Regarding the design of the selection hierarchy (select the left and right changes), it is implemented by the code (replace the column data).
You need to use the reloadAllComponents or reloadComponent: method.
If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!