iOS Foundation-uikit Frame-Advanced View-uipickerview-Instance 1: Order (no relationship between columns and columns)

Source: Internet
Author: User

A. Order
0. Create a plist file with the root array, which contains 3 arrays, each with a heap of food names
Load this plist file
1> declaring Array properties
@property (Nonatomic,strong) Nsarray *foods;
2> lazy Loading (at the end of the implementation file)
#pragma mark-Lazy loading
-(Nsarray *) foods
{
if (_foods = = nil) {
NSString *fullpath = [[NSBundle mainbundle] pathforresource:@ "foods.plist" oftype:nil];
_foods = [Nsarray Arraywithcontentsoffile:fullpath];
}
return _foods;
}

1. Drag a Picker view control, right-click to set the data source to the controller

2. Follow the data source protocol and implement a data source approach
#pragma mark-Data source methods
Return Pickerview How many columns there are altogether
-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview
{
return self.foods.count;
}

Returns how many rows are in column Pickerview component
-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponents: (Nsinteger) component
{
return self.foods[components].count;
}


3. Right-click on the Picker view control to set the proxy as the controller

4. Follow the proxy protocol and implement the Proxy method
#pragma mark-Proxy method
//Return to the row row of column component what is displayed
-(NSString *) Pickerview: (Uipickerview * ) Pickerview Titleforrow: (nsinteger) Row forcomponents: (Nsinteger) component
{
Return self.foods[components][ Row];
}

Second, show à la carte results
1. Drag a view, drag 6 tabs on the view, and connect the label with the scrolling change
2. Implementing a Proxy method (listening on a selected row)
#pragma mark-Proxy method
// When a row of Pickerview is selected, the
//will pass in the selected column and line numbers as parameters
//The
-(void) Pikerview is called only when a row is selected by the finger: (Uipickerview * ) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component
{
//1. Get data for corresponding column rows
NSString * name = Self.foods[component][row];
//2. Determine which column is selected, 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
{
//Beverage
Self.drinkLabel.text = name;
   }

3. Set the default value displayed in Viewdidload (simulate Manual Check)
for (int component = 0; component<self.foods.count;component++)
{
    [self pcikerview:nil didselectrow:0 Incomponent:component];
}

Iii. Random selection of vegetables
1. Drag a view (height 64, including status bar 20), drag 1 buttons and a label on top
Middle and small skill: Y is 64, (same as height), height is 44.
2. Monitor button click (Connect and implement method)
.//here to get Pickerview, call its method, so first connect the Pickerview (Outlet).
#pragma mark-Tap the Monitor button
-(Ibaction) Randomfood: (UIButton *) sender{
Let Pickerview actively select a row
Let Pickerview select Row row for incomponent column
Generate random values based on the number of elements in each column
for (int component = 0;component <self.foods.count;component++)
{
Gets the total number of data for the corresponding column
int total = [self.foods[component] count];
Generates a random number based on the total number of each column (the currently generated random number)
int randomnumber = arc4random ()% Total;

Gets the currently selected row (the row that was last randomly moved to)
int oldrow = [Self.pickerview selectedrowincomponent:0];
Compares the last line number and the currently generated random number, if the same, regenerates
while (Oldrow = = Randomnumber) {
Randomnumber = arc4random ()% Total;
}

Let Pickervier scroll to a line
[Self.pickerview selectrow:randomnumber incomponent:component Animated:yes];

Select a row by code
[Self Pcikerview:nil didselectrow:randomnumber incomponent:component];
}
% 12 is always [0,11], the other is the same, that is, there are several numbers on the%

Q: Why not use [self.foods[2] count] without self.foods[0].count?
Answer: because self.foods[0] = = [Self.foods objectatindex:0];
and Objectatindex: The return value of the method is Id,id is a dynamic type, only when run to know what type, the dynamic type does not have the Count method, so it cannot be called. But to take the old-fashioned way [self.foods[2] count]

iOS Foundation-uikit Frame-Advanced View-uipickerview-Instance 1: Order (no relationship between columns and columns)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.