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}
: