• iPhone控件之UIPickerView2


    1 #import <UIKit/UIKit.h>
    2
    3 @interface UITestViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
    4 {
    5
    6 }
    7
    8 @end
      1 //
    2 // UITestViewController.m
    3 // UITest
    4 //
    5
    6 #import "UITestViewController.h"
    7
    8 NSMutableArray *comp1;
    9 NSMutableArray *comp2;
    10
    11 @implementation UITestViewController
    12
    13 -(void)initComp1
    14 {
    15 comp1 = [[NSMutableArray alloc] init];
    16 UIImageView *imgView;
    17
    18 imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"au.png"]];
    19 [comp1 addObject:imgView];
    20 [imgView release];
    21
    22 imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ca.png"]];
    23 [comp1 addObject:imgView];
    24 [imgView release];
    25
    26 imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gb.png"]];
    27 [comp1 addObject:imgView];
    28 [imgView release];
    29
    30 imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nz.png"]];
    31 [comp1 addObject:imgView];
    32 [imgView release];
    33
    34 imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"us.png"]];
    35 [comp1 addObject:imgView];
    36 [imgView release];
    37 }
    38
    39 -(void)initComp2
    40 {
    41 comp2 = [[NSMutableArray alloc] init];
    42 UILabel *lbl;
    43
    44 lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,44)];
    45 lbl.backgroundColor = [UIColor clearColor];
    46 lbl.text = @"Red";
    47 [comp2 addObject:lbl];
    48 [lbl release];
    49
    50 lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,44)];
    51 lbl.backgroundColor = [UIColor clearColor];
    52 lbl.text = @"Blue";
    53 [comp2 addObject:lbl];
    54 [lbl release];
    55
    56 lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,44)];
    57 lbl.backgroundColor = [UIColor clearColor];
    58 lbl.text = @"Yellow";
    59 [comp2 addObject:lbl];
    60 [lbl release];
    61
    62 lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,44)];
    63 lbl.backgroundColor = [UIColor clearColor];
    64 lbl.text = @"Pink";
    65 [comp2 addObject:lbl];
    66 [lbl release];
    67
    68 lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,44)];
    69 lbl.backgroundColor = [UIColor clearColor];
    70 lbl.text = @"Brown";
    71 [comp2 addObject:lbl];
    72 [lbl release];
    73
    74 lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,44)];
    75 lbl.backgroundColor = [UIColor clearColor];
    76 lbl.text = @"Black";
    77 [comp2 addObject:lbl];
    78 [lbl release];
    79
    80 lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,44)];
    81 lbl.backgroundColor = [UIColor clearColor];
    82 lbl.text = @"White";
    83 [comp2 addObject:lbl];
    84 [lbl release];
    85 }
    86
    87 - (void)viewDidLoad {
    88
    89 [super viewDidLoad];
    90
    91 //create some sample data
    92 [self initComp1];
    93 [self initComp2];
    94
    95 CGRect pickerFrame = CGRectMake(0,120,0,0);
    96
    97 UIPickerView *myPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
    98 myPicker.dataSource = self;
    99 myPicker.delegate = self;
    100 myPicker.showsSelectionIndicator = YES;
    101
    102 [self.view addSubview:myPicker];
    103
    104 [myPicker release];
    105 }
    106
    107 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    108
    109 return 2;
    110 }
    111
    112 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    113
    114 if (component == 0)
    115 return [comp1 count];
    116 else
    117 return [comp2 count];
    118 }
    119
    120 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
    121 {
    122 if (component == 0)
    123 return 100.0;
    124 else
    125 return 200.0;
    126 }
    127
    128 - (UIView *)pickerView:(UIPickerView *)pickerView
    129 viewForRow:(NSInteger)row
    130 forComponent:(NSInteger)component
    131 reusingView:(UIView *)view {
    132
    133 if (component == 0)
    134 return [comp1 objectAtIndex:row];
    135 else
    136 return [comp2 objectAtIndex:row];
    137 }
    138
    139 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    140
    141 NSLog(@"row: %i, component:%i",row,component);
    142 }
    143
    144 - (void)didReceiveMemoryWarning {
    145 // Releases the view if it doesn't have a superview.
    146 [super didReceiveMemoryWarning];
    147
    148 // Release any cached data, images, etc that aren't in use.
    149 }
    150
    151 - (void)viewDidUnload {
    152 // Release any retained subviews of the main view.
    153 // e.g. self.myOutlet = nil;
    154 }
    155
    156
    157 - (void)dealloc {
    158
    159 [comp1 release];
    160 comp1 = nil;
    161
    162 [comp2 release];
    163 comp2 = nil;
    164
    165 [super dealloc];
    166 }
    167
    168 @end



  • 相关阅读:
    树四:遍历二叉树
    树三:创建二叉树
    树二:二叉树定义及性质
    树一:定义及存储
    排序七:归并排序
    排序六:快速排序
    排序五:希尔排序
    排序四:冒泡排序
    深入分析Linux内核链表
    179. Largest Number
  • 原文地址:https://www.cnblogs.com/foxmin/p/2393628.html
Copyright © 2020-2023  润新知