zoukankan      html  css  js  c++  java
  • ios-UIPickerView基本使用

    #import "ViewController.h"
    
    @interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
    {
        NSArray *pickerArray;
    }
    @property (weak, nonatomic) IBOutlet UIPickerView *myPickerView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        _myPickerView.dataSource=self;
        _myPickerView.delegate=self;
        _myPickerView.showsSelectionIndicator=YES;
        pickerArray=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];
       
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        return 3;
    }
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
        return pickerArray.count;
    }
    
    -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
        CGRect rect=CGRectMake(0, 0, [self pickerView:pickerView widthForComponent:row], [self pickerView:pickerView rowHeightForComponent:row]);
        UIView *testView=[[UIView alloc]initWithFrame:rect];
        [testView setBackgroundColor:[UIColor clearColor]];
        [testView setOpaque:YES];
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(8, 0, [self pickerView:pickerView widthForComponent:row]-16.0f, [self pickerView:pickerView rowHeightForComponent:row])];
        [label setBackgroundColor:[UIColor clearColor]];
        label.textAlignment=NSTextAlignmentCenter;
        label.text=pickerArray[row];
        switch (row)
        {
            case 1:
            case 2:
            {
                testView.backgroundColor=component==0?[UIColor greenColor]:[UIColor blueColor];
            }
            case 3:
            {
                testView.backgroundColor=component==0?[UIColor brownColor]:[UIColor redColor];
            }
            break;
            default:
            {
                testView.backgroundColor=component==0?[UIColor grayColor]:[UIColor orangeColor];
            }
            break;
        }
        label.font=[UIFont boldSystemFontOfSize:14.0f];
        [testView addSubview:label];
        return testView;
    }
    
    //可有可无
    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        return pickerArray[row];
    }
    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
    {
        return 120;
    }
    -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
    {
        return 50;
    }
    
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        
         NSLog(@"row=%ld",row);
    }

  • 相关阅读:
    个人理财小助手 —— 简介
    我的分页控件(未完,待续)——控件件介绍及思路
    静态变量 静态对象 静态函数和非静态函数的区别。(我的理解,大家看看对不对)
    通过“访问多种数据库”的代码来学习多态!(.net2.0版)
    Step By Step 一步一步写网站[1] —— 填加数据
    个人理财小助手 —— 数据库(一)
    几个鸟叫的声音
    Step By Step 一步一步写网站[1] —— 帧间压缩,表单控件
    面向对象相关
    论程序的成长—— 你写的代码有生命力吗?
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4153204.html
Copyright © 2011-2022 走看看