#import "DengViewController.h" @interface DengViewController () @end @implementation DengViewController - (void)viewDidLoad { [super viewDidLoad]; CGRect rect=CGRectMake(0, 0,320, 220); _showlabel=[[UILabel alloc] initWithFrame:rect]; _showlabel.backgroundColor=[UIColor grayColor]; _showlabel.textAlignment=NSTextAlignmentCenter; _showlabel.font=[UIFont systemFontOfSize:18]; [self.view addSubview:_showlabel]; UIPickerView *pickerview = [[UIPickerView alloc] init]; CGRect fram =pickerview.frame; fram.origin.y=self.view.bounds.size.height-fram.size.height; pickerview.frame=fram; pickerview.showsSelectionIndicator=YES; pickerview.backgroundColor=[UIColor yellowColor]; pickerview.delegate=self; pickerview.dataSource=self; [self.view addSubview:pickerview]; [pickerview reloadAllComponents]; [self pickerView:pickerview didSelectRow:0 inComponent:0]; // Do any additional setup after loading the view, typically from a nib. } - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 2; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return 9; } - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ return 30; } - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ return 80; } - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ if (view==nil) { CGFloat width = [self pickerView:pickerView widthForComponent:component]; CGFloat heigh = [self pickerView:pickerView rowHeightForComponent:row]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, heigh)]; label.textAlignment=NSTextAlignmentCenter; label.font=[UIFont systemFontOfSize:16]; view=label; } UILabel *label =(UILabel*)view; label.text=[NSString stringWithFormat:@"%d",row+1]; if (row%2!=1) { label.backgroundColor=[UIColor redColor]; } return label; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ NSInteger row0 =[pickerView selectedRowInComponent:0]; NSInteger row1 =[pickerView selectedRowInComponent:1]; UILabel *label1=(UILabel *)[pickerView viewForRow:row0 forComponent:0]; UILabel *label2=(UILabel *)[pickerView viewForRow:row1 forComponent:1]; int result =label1.text.intValue*label2.text.intValue; _showlabel.text=[NSString stringWithFormat:@"%d",result]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end