iOS自定義的PickView

weixin_33782386發表於2017-03-29

由兩個檔案

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);
}
3729815-02dece2a564e0e7e.png
QQ20170329-181340.png

相關文章