码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发日记48-详解UIPickerView

时间:2015-11-26 12:39:51      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

今天博主有一个UIPickerView的需求,遇到了一些困难点,在此和大家分享,希望能够共同进步.

UIPickerView是一个选择器控件,它比UIDatePicker更加通用,它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活.

UIPickerView直接继承了UIView,没有继承UIControl,因此,它不能像UIControl那样绑定事件处理方法,UIPickerView的事件处理由其委托对象完成.

    self.viewOfPick=[[UIPickerView alloc]initWithFrame:CGRectMake(100, 0, 200, [UIScreen mainScreen].bounds.size.height)];

    _viewOfPick.dataSource=self;

    _viewOfPick.delegate=self;

//pickerView默认选中row为0,无线滚动只是在一开始的时候显示row的中间值,造成无线滚动的假象

    [_viewOfPick selectRow:9 inComponent:0 animated:YES];

//设置pickerView默认选中最后一行

    [_viewOfPick selectRow:9 inComponent:1 animated:YES];

    [self.view addSubview:_viewOfPick];

//类似tableView的cell for row,根据row和component返回字符串显示

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED

{

    return @"空";

}

//类似tableView的didSelect,根据row和component进行具体的传值等操作

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED

{

    NSLog(@"*********%ld,%ld",(long)component,row);

}

//类似tableView的cell for row,根据row和component返回自定义视图显示

//注意,同时实现返回NSString 和下面这个 返回UIView的方法,只执行返回UIView的方法

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view __TVOS_PROHIBITED

{

    UIView *blackView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];

    blackView.backgroundColor=[UIColor blackColor];

    return blackView;

}

//返回pickerView的行高

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component __TVOS_PROHIBITED

{

    return 40;

}

// returns the number of ‘columns‘ to display.component的数量

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    return 2;

}

 

// returns the # of rows in each component..row的数量

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

    return 10;

}

 

 

iOS开发日记48-详解UIPickerView

标签:

原文地址:http://www.cnblogs.com/Twisted-Fate/p/4997212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!