Cat Share, must boutique
Material code Address: http://blog.csdn.net/u013357243/article/details/45057267
Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243?viewmode=contents
Look First # #
Uipickerview Control Uipickerview Use:
Used to show many rows (row) of many columns (component) of data, more for e-commerce à la carte, City selection and so on.
Uipickerview usage:
He used to be similar to TableView, usage:
1: Set up proxies and data sources
@interface NYViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>
Data Source: Uipickerviewdatasource,
1, returns how many columns
2, returns how many rows
#pragma mark-uipickerviewdatasource//Return Pickerview How many columns are there altogether?- (Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{//return 3; return Self. Foods. Count;}//Returns the number of rows in column component of Pickerview- (Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component{//return 4; //1. Get an array of corresponding columns Nsarray*subfoods = Self. Foods[Component];//2. Returns the number of rows for the corresponding column returnSubfoods. Count;}
Agent Uipickerviewdelegate
Returns what is shown on row row of column component
#pragma mark - UIPickerViewDelegate// 返回第component列的第row行显示什么内容- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ // 1.获取对应列的数组 NSArrayself.foods[component]; // 2.获取对应行的标题 NSString *name = subFoods[row]; return name;}
How to monitor which line is selected
Didselectrow
When we select a row in a column, we set the corresponding data.
//When a line of Pickerview is selected, call//The selected column number and line number are passed as parameters//will only be called when a row is selected by Finger- (void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (Nsinteger) Row incomponent: (Nsinteger) component{//NSLog (@ "component =%d, row =%d", component, row); //1. Get the data for the corresponding column row NSString*name = Self. Foods[Component] [Row];//NSLog (@ "name =%@", name); //2. Determine which column is selected, and set the corresponding data according to the column number if(0= = component) {//Fruit Self. Fruitlabel. Text= name; }Else if(1= = component) {//Main course Self. Staplelabel. Text= name; }Else{//Beverages Self. Drinklabel. Text= name; }}
Implementing Random Events
How to get Pickerview to scroll to its own line
SelectRow Incomponent let Pickerview actively scroll to a row of a column
- (ibaction) Randomfood: (UIButton*) Sender {//Let Pickerview actively select a row //Let Pickerview select Row row for incomponent column//[Self.pickerview selectrow:1 incomponent:0 Animated:yes]; /* [Self.pickerview selectrow:arc4random ()% incomponent:0 Animated:yes]; [Self.pickerview selectrow:arc4random ()% incomponent:1 Animated:yes]; [Self.pickerview selectrow:arc4random ()% incomponent:2 Animated:yes]; *///[Self.foods objectatindex:0]; = = Self.foods[0];//[self.foods[0] count]; /*//Generate random values based on the number of elements in each column [Self.pickerview selectrow:arc4random ()% [self.foods[0] count] incomponent:0 Animated:ye S]; [Self.pickerview selectrow:arc4random ()% [self.foods[1] count] incomponent:1 Animated:yes]; [Self.pickerview selectrow:arc4random ()% [self.foods[2] count] incomponent:2 Animated:yes]; */ for(intComponent =0; Component < Self. Foods. Count; component++) {//Get the total number of data for the corresponding column intTotal = [ Self. Foods[Component] count];generate random numbers based on the total number of each column (the currently generated random number) intRandomnumber = arc4random ()% Total;//Gets the currently selected row (the row that was last randomly moved to) intOldrow = [ Self. PickerviewSelectedrowincomponent:0];//NSLog (@ "Oldrow =%d", oldrow); //compares the last line number with the currently generated random number if the same rebuild while(Oldrow = = Randomnumber) {randomnumber = Arc4random ()% Total; }//Let Pickerview scroll to a line[ Self. PickerviewSelectrow:randomnumber incomponent:component Animated:YES];//Select a row by code[ SelfPickerview:NilDidselectrow:randomnumber Incomponent:component]; }}
PS: New iOS Exchange Learning Group: 304570962 can add cat qq:1764541256 or Znycat Let's study hard together.
Sanayu's Blog
Address: http://blog.csdn.net/u013357243?viewmode=contents
Cat learning iOS (20) UI Uipickerview_ ordering system