iOS自定义的PickView
来源:互联网 发布:跨境电商大数据平台 编辑:程序博客网 时间:2024/05/23 22:36
由两个文件
1、XDPickView.h
2、ViewController.h
下边上代码
1.1 //// XDPickView.h// HEAL//// Created by 窦心东 on 2017/3/29.// Copyright © 2017年 窦心东. All rights reserved.//#import <UIKit/UIKit.h>@protocol XDPickerDelegate <NSObject>@required/** * 选择的选项的代理方法 必须实现 */- (void)PickerSelectorIndixString:(NSString *)str;@end@interface XDPickView : UIView@property (nonatomic,assign)id<XDPickerDelegate>delegate;/** 数据源数组 */@property (nonatomic,strong) NSMutableArray *pickViewTextArray;/** pickview的背景颜色 */@property (nonatomic,strong) UIColor *backgroundColor;/** 文字的颜色 */@property (nonatomic,strong) UIColor *contentTextColor;/** 列宽 */@property (nonatomic,assign) CGFloat LieWidth;//默认选择的哪一个- (void)MoRenSelectedRowWithObject:(id)object;@end
1.2//// XDPickView.m// HEAL//// Created by 窦心东 on 2017/3/29.// Copyright © 2017年 窦心东. All rights reserved.//#import "XDPickView.h"@interface XDPickView ()<UIPickerViewDataSource,UIPickerViewDelegate>/** 数组 */@property (nonatomic,strong) NSMutableArray *proTitleList;/** // 选择框 UIPickerView *pickerView */@property (nonatomic,strong) UIPickerView *pickerView ;@end@implementation XDPickView-(NSMutableArray *)proTitleList{ if (_proTitleList == nil) { _proTitleList = [NSMutableArray array]; for (int i = 100; i<500; i++) { [_proTitleList addObject:[NSString stringWithFormat:@"%d",i]]; } } return _proTitleList;}-(UIPickerView *)pickerView{ if (_pickerView == nil) { _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; _pickerView.showsSelectionIndicator=YES; _pickerView.dataSource = self; _pickerView.delegate = self; } return _pickerView;}- (instancetype)init{ self = [super init]; if (self) { [self initData]; } return self;}- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { [self initData]; } return self;}- (void)initData{ //初始化 [self creatPickView];}//创建pickview- (void)creatPickView{ [self addSubview:self.pickerView];}//设置PickView的背景颜色-(void)setBackgroundColor:(UIColor *)backgroundColor{ _backgroundColor = backgroundColor; if (_backgroundColor) { self.pickerView.backgroundColor = _backgroundColor; } else { self.pickerView.backgroundColor = ColorWithHEAL; }}#pragma mark - 设置字体颜色-(void)setContentTextColor:(UIColor *)contentTextColor{ _contentTextColor = contentTextColor; if (_contentTextColor == nil) { _contentTextColor = [UIColor whiteColor]; }}#pragma mark - 设置数据源数组-(void)setPickViewTextArray:(NSMutableArray *)pickViewTextArray{ _pickViewTextArray = pickViewTextArray; if (_pickViewTextArray == nil) { _pickViewTextArray = self.proTitleList; }}-(void)setLieWidth:(CGFloat)LieWidth{ _LieWidth = LieWidth; if (_LieWidth < 40) { _LieWidth = 180; }}#pragma mark - 默认选中的是- (void)MoRenSelectedRowWithObject:(id)object{ if (object == nil) { return; } NSInteger row = [_pickViewTextArray indexOfObject:object]; [self.pickerView selectRow:row inComponent:0 animated:YES];}#pragma mark - 改变分割线的颜色- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ //设置分割线的颜色 for(UIView *singleLine in pickerView.subviews) { if (singleLine.frame.size.height < 1) { singleLine.backgroundColor = [UIColor clearColor];//取消分割线 } } //设置文字的属性 UILabel *Label = [UILabel new]; Label.textAlignment = NSTextAlignmentCenter; Label.text = _pickViewTextArray[row]; Label.textColor = _contentTextColor?_contentTextColor:[UIColor whiteColor]; return Label;}#pragma mark - UIPickerViewDataSource 相关代理#pragma Mark -- 列数- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1;}#pragma mark - pickerView 每列个数- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return _pickViewTextArray.count;}#pragma mark - UIPickerViewDelegate 相关代理方法// 每列宽度- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { return _LieWidth?_LieWidth:180;}#pragma mark - 返回当前行cell的内容,此处是将数组中数值添加到滚动的那个显示栏上-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ return _pickViewTextArray[row];}#pragma mark - 返回选中的行didSelectRow- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ NSString *selectRowString = _pickViewTextArray[row]; NSLog(@"选中的是====%@",selectRowString); if ([self.delegate respondsToSelector:@selector(PickerSelectorIndixString:)]) { //如果我的代理响应这个方法的话 就去调用这个代理方法 [self.delegate PickerSelectorIndixString:selectRowString]; }}- (void)dealloc{}/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect { // Drawing code}*/@end
2.1//// ViewController.h// HEAL//// Created by 窦心东 on 2017/1/18.// Copyright © 2017年 窦心东. All rights reserved.//#import "ViewController.h"@interface ViewController : UItViewController@end
2.2//// ViewController.m// HEAL//// Created by 窦心东 on 2017/1/18.// Copyright © 2017年 窦心东. All rights reserved.//#import "ViewController.h"#import "XDPickView.h"@interface ViewController ()<XDPickerDelegate>/** 数据源数组 */@property (nonatomic,strong) NSMutableArray *proTitleList;@end@implementation ViewController-(NSMutableArray *)proTitleList{ if (_proTitleList == nil) { _proTitleList = [NSMutableArray array]; for (int i=100; i<500; i++) { [_proTitleList addObject:[NSString stringWithFormat:@"%d",i]]; } } return _proTitleList;}-(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated];}-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated];}- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor greenColor]; //实例 XDPickView *pick = [[XDPickView alloc] initWithFrame:CGRectMake(20, 100, SCREEN_WIDTH-40, 200)]; pick.backgroundColor = ColorWithHEAL; pick.contentTextColor = [UIColor yellowColor]; pick.LieWidth = 80; pick.delegate = self; [self.view addSubview:pick]; pick.pickViewTextArray = self.proTitleList;//设置数据 [pick MoRenSelectedRowWithObject:@"200"];//默认选中的哪一项}#pragma mark -- XDPickViewDelegate 的代理方法必须实现-(void)PickerSelectorIndixString:(NSString *)str{ NSLog(@"代理方法实现的选中的是%@",str);}
阅读全文
0 0
- iOS自定义的PickView
- 自定义pickview的高度
- 自定义pickview的高度
- 自定义pickview的高度
- IOS 学习 PickView的使用
- IOS的PickView作为TextField事件
- android 自定义控件pickview
- IOS初学:使用PickView中遇到的问题
- PickView简单的依赖
- pickView的用法
- 开发中仿IOS三级菜单,开源项目PickView实用自定义数据封装使用实例
- pickView的菜单选择代码
- Android中PIckView的使用
- iOS开发--PickView 选取器笔记
- PickView选中某一行的字体变化
- 隐藏pickview 的分割线 分界线
- pickeview 的用法 collection定制化pickview
- 1103pickview
- jdbc 应用简述
- JAVA中如何跳出当前的多重嵌套循环
- va_list
- memset和循环的比较
- phpcms v9站群跨站点、多站点推荐位调用功能
- iOS自定义的PickView
- Flash终于走向死亡 数十年兴衰之路你知多少
- sublime text软件的安装及配置
- 数据结构实验之图论七:驴友计划(Dijkstra)
- vue按需加载组件-webpack require.ensure
- js随笔
- 剑指offer:矩形覆盖
- MySQL数据库安装与配置详解
- scikit-learn功能大全