Cat Share, must boutique
Material code Address: http://download.csdn.net/detail/u013357243/8596279
Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243?viewmode=contents
Look First # #
Code
////NJVIEWCONTROLLER.M//01-à la carte////Created by Apple on 14-6-3.//Copyright (c) 2014 Heima. All rights reserved.//#import "NJViewController.h" @interface njviewcontroller () <Uipickerviewdatasource, uipickerviewdelegate >@property(Weak,nonatomic)IboutletUipickerview *pickerview;/** * Random button click event */- (ibaction) Randomfood: (UIButton*) sender;/** * All food * *@property(nonatomic,Strong)Nsarray*foods;/** * Fruit * *@property(Weak,nonatomic)Iboutlet UILabel*fruitlabel;/** * Main Course * *@property(Weak,nonatomic)Iboutlet UILabel*staplelabel;/** * Drinks * * *@property(Weak,nonatomic)Iboutlet UILabel*drinklabel;@end @implementation njviewcontroller - (void) viewdidload{[SuperViewdidload];//Set the default selection//Self.fruitLabel.text = self.foods[0][0];//Self.stapleLabel.text = self.foods[1][0];//Self.drinkLabel.text = self.foods[2][0];//[self pickerview:nil didselectrow:0 incomponent:0];//[self pickerview:nil didselectrow:0 incomponent:1];//[self pickerview:nil didselectrow:0 incomponent:2]; for(intComponent =0; Component < Self. Foods. Count; component++) {[ SelfPickerview:NilDidselectrow:0Incomponent:component]; }}#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;}#pragma mark-uipickerviewdelegate//Return to the row row of column component what is displayed- (NSString*) Pickerview: (Uipickerview *) Pickerview Titleforrow: (Nsinteger) Row forcomponent: (Nsinteger) component{//1. Get an array of corresponding columns Nsarray*subfoods = Self. Foods[Component];//2. Get the title of the corresponding row NSString*name = Subfoods[row];returnName;}//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; }}#pragma mark-Lazy loading- (Nsarray*) foods{if(_foods = =Nil) {NSString*fullpath = [[NSBundleMainbundle] pathforresource:@"Foods.plist"OfType:Nil]; _foods = [NsarrayArraywithcontentsoffile:fullpath]; }return_foods;}#pragma mark-tap the Monitor button- (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]; }}@end
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 learns iOS (20) UI Uipickerview_ ordering system