Use UIPickerView (3) and use uipickerview

Source: Internet
Author: User

Use UIPickerView (3) and use uipickerview

 

The usage of UIPickerView (I) and UIPickerView (II) in the first two articles learned how to use the single-column selector and double-row selector of UIPickerView.

Now let's learn the interdependent multi-column selector.

1. Compliance with the agreement

2. Create a pickView

 

3. Implementation Protocol
// The method defined in UIPickerViewDataSource. the return value of this method determines the number of columns in the control-(NSInteger) numberOfComponentsInPickerView (UIPickerView *) pickerView {return 2; // 2 indicates that the control only contains 2 columns} // the method defined in UIPickerViewDataSource. the return value of this method determines the number of list items in the specified column of the control-(NSInteger) pickerView :( UIPickerView *) pickerView numberOfRowsInComponent :( NSInteger) component {// because the control only contains one column, you do not need to care about the column Number Parameter component // This method returns teams. count indicates how many elements the teams contains and how many rows the control contains. if (component = 0) {re Turn _ areas. count;} else return [[_ teams objectForKey: _ selectedAreas] count];} // method defined in UIPickerViewDelegate, NSString returned by this method will be used as the title text of the UIPickerView // specified column and list item-(NSString *) pickerView :( UIPickerView *) pickerView titleForRow :( NSInteger) row forComponent :( NSInteger) component {// because the control only contains one column, you do not need to care about the column Number Parameter component // This method returns the elements in teams based on the row parameter. The row parameter represents the number of the list item, // Therefore, this method indicates the items in the list. Use the element if (component = 0) {re in teams. Turn [_ areas objectAtIndex: row];} return [[_ teams objectForKey: _ selectedAreas] objectAtIndex: row];} // This method is triggered when the user selects the specified column and list items in UIPickerViewDataSource-(void) pickerView :( UIPickerView *) pickerView didSelectRow :( NSInteger) row inComponent :( NSInteger) component {if (component = 0) {_ selectedAreas = [_ areas objectAtIndex: row]; [self. pickView reloadComponent: 1];} NSArray * tmp = component = 0? _ Areas: [_ teams objectForKey: _ selectedAreas]; NSString * tip = component = 0? @ "Region": @ "team"; // use a UIAlertView to display the selected list item UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "prompt" message: [NSString stringWithFormat: @ "your selected % @ is: % @", tip, [tmp objectAtIndex: row] delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil]; [alert show];} // the method defined in UIPickerViewDelegate. The NSString returned by this method is used as the width of the specified column in // UIPickerView-(CGFloat) pickerView :( UIPickerView *) pickerView widthForComponent :( NSInteger) component {// if it is the first column, the width is 90 if (component = 0) {return 90;} return 210; // for other columns (only the second column), the width is 210}

 

:

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.