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];}
JHPickView是一款用于iOS开发的选择器组件,封装了系统提供的UIPickView和UIDatePickView,简化了调用代码并支持一定程度的定制。本文介绍如何使用JHPickView并实现日期与星座的关联。
8952

被折叠的 条评论
为什么被折叠?



