JHPickView — 一款灵活的选择器

JHPickView — 一款灵活的选择器

选择器作为iOS开发过程中是经常使用到的控件,系统为我们提供了UIPickView和UIDatePickView 两种选择器。但是使用起来较为麻烦。
本文就是博主整合系统的两种‘选择器进行封装,简化了调用的代码,使用起来简单,并且一定程度上支持定制。
- github 地址:JHPickView
- 码云 地址:JHPickView
使用时只需要导入头文件 #import “JHPickView.h”

初始化

JHPickView *picker = [[JHPickView alloc]initWithFrame:self.view.bounds];
picker.delegate = self ;
picker.arrayType = GenderArray; //类型
[self.view addSubview:picker];

日期关联星座

pragma mark - JHPickerDelegate
  • 选择器代理事件

-(void)PickerSelectorIndixString:(NSString *)str
{
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:self.selectedIndexPath] ;
cell.detailTextLabel.text = str ;
// 根据选择的日期匹配星座
if (self.selectedIndexPath.row == 1) {
NSDateFormatter* formater = [[NSDateFormatter alloc] init];
formater.dateFormat = @”yyyy年MM月d日” ;
NSDate* date = [formater dateFromString:str];
NSDateComponents* components = [self getDateComponentsFromDate:date];
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedIndexPath.row+1 inSection:self.selectedIndexPath.section]] ;
cell.detailTextLabel.text = str ;
}
}

  • 根据日期获取 NSDateComponents

    -(NSDateComponents*)getDateComponentsFromDate:(NSDate*)date{

    // NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @”星期天”, @”星期一”, @”星期二”, @”星期三”, @”星期四”, @”星期五”, @”星期六”, nil];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@”Asia/Beijing”];
    [calendar setTimeZone: timeZone];
    NSCalendarUnit calendarUnit = NSYearCalendarUnit |
    NSMonthCalendarUnit |
    NSDayCalendarUnit |
    NSWeekdayCalendarUnit |
    NSHourCalendarUnit |
    NSMinuteCalendarUnit |
    NSSecondCalendarUnit;
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:date];
    return theComponents ;
    }

  • 根据几月几号匹配星座

    -(NSString *)getAstroWithMonth:(NSInteger)m day:(NSInteger)d{ NSString *astroString = @”魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯”;
    NSString *astroFormat = @”102123444543”;
    NSString *result;`

    if (m<1||m>12||d<1||d>31){
    return @”错误日期格式!”;
    }
    if(m==2 && d>29)
    {
    return @”错误日期格式!!”;
    }else if(m==4 || m==6 || m==9 || m==11) {
    if (d>30) {
    return @”错误日期格式!!!”;
    }
    }
    result=[NSString stringWithFormat:@”%@”,[astroString substringWithRange:NSMakeRange(m*2-(d < [[astroFormat substringWithRange:NSMakeRange((m-1), 1)] intValue] - (-19))*2,2)]];
    return [NSString stringWithFormat:@”%@座”,result];}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值