UIPickerView _ ordering system for IOS (20) UI of cat learning, iosuipickerview

Source: Internet
Author: User

UIPickerView _ ordering system for IOS (20) UI of cat learning, iosuipickerview

CAT/CAT sharing, must be excellent

Material address: http://blog.csdn.net/u013357243/article/details/45057267
For Original Articles, please reprint them. Reprinted Please note: Yan Nai-yu's blog
Http://blog.csdn.net/u013357243? Viewmode = contents

First look ##

UIPickerView:

It is used to display data of many rows and many columns (component), mostly for e-commerce ordering, city selection, and so on.

UIPickerView usage:

It is similar to tableView. Usage:

1: Set proxy and Data Source
@interface NYViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>
Data source: UIPickerViewDataSource,

1. How many columns are returned?
2. How many rows are returned?

# Pragma mark-UIPickerViewDataSource // return the total number of columns in the pickerView-(NSInteger) numberOfComponentsInPickerView :( UIPickerView *) pickerView {// return 3; return self. foods. count;} // return the number of rows in the component column of the pickerView-(NSInteger) pickerView :( UIPickerView *) pickerView numberOfRowsInComponent :( NSInteger) component {// return 4; // 1. obtain the array NSArray * subFoods = self. foods [component]; // 2. returns the number of rows in the corresponding column. return subFoods. count ;}
Proxy UIPickerViewDelegate

Returns the row content of the component column.

# Pragma mark-UIPickerViewDelegate // return what is displayed in the row of the component column-(NSString *) pickerView :( UIPickerView *) pickerView titleForRow :( NSInteger) row forComponent :( NSInteger) component {// 1. obtain the array NSArray * subFoods = self. foods [component]; // 2. obtain the line title NSString * name = subFoods [row]; return name ;}
How to monitor which row to select

DidSelectRow
When we select a row in a column, we set the corresponding data.

// When a row of pickerView is selected, it is called. // The selected column number and row number are passed as parameters. // It is called only when a row is selected by fingers -( void) pickerView :( UIPickerView *) pickerView didSelectRow :( NSInteger) row inComponent :( NSInteger) component {// NSLog (@ "component = % d, row = % d", component, row ); // 1. obtain the data NSString * name = self. foods [component] [row]; // NSLog (@ "name = % @", name); // 2. determine which column is selected, and set the corresponding data if (0 = component) {// Fruit self according to the column number. fruitLabel. text = name;} else if (1 = component) {// main course self. stapleLabel. text = name;} else {// beverage self. drinkLabel. text = name ;}}
Implement random events

How to Make pickerView scroll to which line
SelectRow inComponent enables pickerView to actively scroll to a row in a column

-(IBAction) randomFood :( UIButton *) sender {// Let the pickerView actively select a Row. // Let the pickerView select the Row of the inComponent column. // [self. pickerView selectRow: 1 inComponent: 0 animated: YES];/* [self. pickerView selectRow: arc4random () % 12 inComponent: 0 animated: YES]; [self. pickerView selectRow: arc4random () % 15 inComponent: 1 animated: YES]; [self. pickerView selectRow: arc4random () % 10 inComponent: 2 animated: YES]; * // [self. foods objectAtIndex: 0]; = self. foods [0]; // [self. foods [0] count]; // generates 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]; */for (int component = 0; component <self. foods. count; component ++) {// obtain the total data of the corresponding column int total = [self. foods [component] count]; // generate a random number based on the total number of each column (the current Random Number) int randomNumber = arc4random () % total; // obtain the currently selected row (the row to be moved after the last random operation) int oldRow = [self. pickerView selectedRowInComponent: 0]; // NSLog (@ "oldRow = % d", oldRow); // compare whether the last row number is the same as the currently generated random number, if the while (oldRow = randomNumber) {randomNumber = arc4random () % total;} // Let the pickerview scroll to a row [self. pickerView selectRow: randomNumber inComponent: component animated: YES]; // select a line using the code [self pickerView: nil didSelectRow: randomNumber inComponent: component];}

Ps: new iOS communication learning group: 304570962 can be added to cat qq: 1764541256 or znycat. Let's study hard together.
Yan Nai-yu's blog
Http://blog.csdn.net/u013357243? Viewmode = contents

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.