Use Picker view in IOS development to implement a UI example for a la carte application _ios

Source: Internet
Author: User

First, the realization effect

Note: Click on the Random button, can automatically select, the bottom of the data automatically refreshed.

Second, the realization of ideas

1.picker view has a default height of 162 and cannot be modified.
2. Display data, you need to set up a data source, there are two ways (become a data source, Compliance Agreement)
3. Implementing two methods in a data source
1) Return a total number of columns
2 How many rows are there in this column?
4. Tell it by proxy which row of that column shows which data (set its proxy as Controller)
5. Use lazy load, load all the food
6. Complete the presentation of the basic data (columns, rows, content)
7. Automatically update the selected food information. (Use a large view with 6 labels above)
1 Assign value to 3 lab, add three attributes (fruit, main course, beverage)
2 listening to the selection of which line (monitoring has two ideas, one is the agent, one is the notification), first see if there is no proxy method (Didselectrow) This method when a row is selected, the selected column number and line number as parameters are passed in. Can get the corresponding column number and line number.
3 The Listening method that is invoked when the check is completed
4 in Viewdidload to set the default selection, set to [0][1]
5 Improve scalability (manually call those lines-use a For loop)
8. Realization of stochastic function
1 How to get the code to select a row (SelectRow), call the method to specify which row to scroll into that column
2 Implement the function of the head (use a large uiview, put two child controls inside)
3 Set height 44, how to center the position of the random button? It can be set to a height of 44, with a maximum Y value of 64.
4 Set the Random button click event Randomfood, let Pickerview actively select a row.
5 method of generating random number (generate random number limit, no more than current total)
6 disadvantage, the future data changes, will be the error (modulo a few) [Self.foods[0] count]? Why not use shorthand syntax? (Remember to keep in mind)
7 random number of processing is not rigorous, sometimes generated random number may be equal, so the column doesn't scroll, gets the total number of data in the corresponding column, how to get the last random value (that is, the currently selected row), compares the previous line number with the current generated random number, and if the same overrides the build
9. To solve another problem, the following data random refresh is invalid, through the code to select a row.

Iii. Implementing code Examples
1. Project document structure and storyboard file

Storyboard file Large interface settings:

2. code example
Master Controller File Code:

Copy Code code as follows:

//
Yyviewcontroller.m
06-The realization of simple vegetable selection system
//
Created by Apple on 14-6-5.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYViewController.h"

Compliance with data sources and agent agreements
@interface Yyviewcontroller () <UIPickerViewDataSource,UIPickerViewDelegate>
/**
* Fruit
*/
@property (Strong, nonatomic) Iboutlet Uilabel *fruitlab;
/**
* Main Course
*/
@property (Strong, nonatomic) Iboutlet Uilabel *staplelab;
/**
* Drink
*/
@property (Strong, nonatomic) Iboutlet Uilabel *drinklab;
/**
* Save all the data
*/
@property (Nonatomic,strong) Nsarray *foods;
@property (Weak, nonatomic) Iboutlet Uipickerview *pickerview;
-(Ibaction) Randomfood: (ID) sender;

@end


Copy Code code as follows:

@implementation Yyviewcontroller

-(void) viewdidload
{
[Super Viewdidload];

Here set the initial display of the Data refresh section below
for (int component = 0; component<self.foods.count; component++) {
[Self Pickerview:nil didselectrow:0 incomponent:component];
}
}

#pragma mark-use lazy load to load data information
-(Nsarray *) foods
{
if (_foods==nil) {
NSString *fullpath=[[nsbundle mainbundle]pathforresource:@ "Foods.plist" oftype:nil];
Nsarray *arraym=[nsarray Arraywithcontentsoffile:fullpath];
_foods=arraym;
}
return _foods;
}

#pragma mark-Handle the Click event of a random button
-(Ibaction) Randomfood: (ID) Sender {

Let Pickerview actively select a row
Let Pickerview select the row row for the 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 a random value based on the number of elements in each column
[Self.pickerview selectrow:arc4random ()% [self.foods[0] count] incomponent:0 Animated:yes];
[Self.pickerview selectrow:arc4random ()% [self.foods[1] count] incomponent:1 Animated:yes];
[Self.pickerview selectrow:arc4random ()% [self.foods[2] count] incomponent:2 Animated:yes];
*/

Set a random number
for (int component=0; component<self.foods.count; component++) {
Gets the number of data elements that correspond to the current column
int total=[self.foods[component] count];
Generate random numbers based on the total number of each column (currently generated random numbers)
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 previous line number with the current generated random number, and regenerates if the same
while (Oldrow==randomnumber) {
Randomnumber=arc4random ()%total;
}

Let Pickerview scroll to a specified row
[Self.pickerview selectrow:randomnumber incomponent:component Animated:yes];
Simulate, select a row by code
[Self Pickerview:nil didselectrow:randomnumber incomponent:component];
}
}

Setting data #pragma mark-
How many columns are there altogether?
-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview
{
return self.foods.count;
}

How many rows each column corresponds to
-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component
{
1. Get the current column
Nsarray *araym= Self.foods[component];
2. Returns the number of rows corresponding to the current column
return araym.count;
}

What data is displayed for each row of each column
-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component
{
1. Get the current column
Nsarray *araym= Self.foods[component];
2. Gets the data for the row of the current column
NSString *name=araym[row];
return name;
}

#pragma mark-settings below the data refresh
Called when a row of Pickerview is selected
The selected column number and line number are passed in as arguments
Called only when a line is selected by a finger
-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component
{
Get the corresponding column, the corresponding row of data
NSString *name=self.foods[component][row];
assigning values
if (0==component) {
Self.fruitlab.text=name;
}else if (1==component)
{
Self.staplelab.text=name;
}else
Self.drinklab.text=name;
}

#pragma mark-Hide Status bar
-(BOOL) Prefersstatusbarhidden
{
return YES;
}
@end


Iv. Important Additions

Please note why [Self.foods[0] count is used in the code implementation; Instead of using the DOT syntax self.foods[0].count to take a value.

[Self.foods objectatindex:0]; = = self.foods[0];//The effect of the two sentences is equivalent, and self calls objectatindex:0 this method, return is an ID type of the universal pointer, its true type to the actual operation can be detected, so can not directly use Self.foods [0].count.

Related Article

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.