iOS自定義的PickView
由兩個檔案
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);
}
相關文章
- iOS筆記--PickView選擇國家iOS筆記View
- iOS自定義tabBariOStabBar
- iOS 自定義 UISlider 的 trackRectiOSUIIDE
- iOS-自定義PickerViewiOSView
- iOS 自定義內容的彈窗iOS
- iOS自定義UIView動畫效果iOSUIView動畫
- iOS自定義UIPageControliOSUI
- iOS 自定義進度條iOS
- 【iOS】關於 UICollectionView 的自定義佈局iOSUIView
- iOS探索 KVO原理及自定義iOS
- iOS探索 KVC原理及自定義iOS
- iOS自定義控制元件 AlertViewiOS控制元件View
- iOS自定義控制元件 SegmentiOS控制元件
- ios自定義圓環進度條iOS
- iOS自定義 Transitions 動畫總結iOS動畫
- iOS 自定義 AlertController 分享iOSController
- iOS使用xib自定義UIViewiOSUIView
- 【IOS初學者】UITableView與自定義UITableViewCelliOSUIView
- iOS 自定義鍵盤字母按鈕iOS
- iOS自定義控制元件 SlideriOS控制元件IDE
- iOS自定義拍照框拍照&裁剪(一)iOS
- ios自定義簡約導航欄iOS
- Cordova學習--iOS自定義外掛iOS
- iOS 在 App 中使用自定義字型iOSAPP自定義字型
- iOS開發 AVFoundation自定義相機iOS
- iOS 自定義字型出問題啦!iOS自定義字型
- Xamarin iOS教程之自定義檢視iOS
- [iOS 10 day by day] Day 6:自定義的通知介面iOS
- iOS實現自定義的彈出檢視(popView)iOSView
- iOS 新增自定義的字型 Fonts provided by applicationiOSIDEAPP
- iOS自定義控制元件:自定義TableView、CollectionView空資料佔點陣圖iOS控制元件View
- iOS 自定義雙向滑塊SlideriOSIDE
- iOS OC-自定義TabBar TabBarViewControlleriOStabBarViewController
- iOS自定義log並寫入檔案iOS
- iOS自定義轉場動畫(push、pop動畫)iOS動畫
- iOS 自定義tableView Cell、高度自適應iOSView
- IOS 自定義 UIDatePicker 背景圖片iOSUI
- iOS 12 通知新特性 —— 自定義 App 通知的外觀iOSAPP