cell 中textFile 的inputView inputAccessoryView

来源:互联网 发布:vb select语句使用方法 编辑:程序博客网 时间:2024/05/16 09:51

//在项目中添加了选择生日 ,性别



            cell.textField.inputView = self.datePicker;
            cell.textField.inputAccessoryView = self.toolbar;



/** DatePicker */
- (void)createTheDatePicker
{
    // 选择生日
    _datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 200)];
    [_datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
    _datePicker.backgroundColor = [UIColor whiteColor];
    _datePicker.maximumDate = [NSDate date];
    [_datePicker setDatePickerMode:UIDatePickerModeDate];
    
    _toolbar = [[UIToolbar alloc]init];
    
    _toolbar.backgroundColor = [UIColor blackColor];//kDefaultColor_separation;
    _toolbar.barTintColor =kDefaultColor_sixthree;
    _toolbar.frame = CGRectMake(0, 0, KScreenWidth, 40);
    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)];
    
    UIBarButtonItem *emptyItem1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *emptyItem2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIBarButtonItem *comfirmItem = [[UIBarButtonItem alloc]initWithTitle:@"确认" style:UIBarButtonItemStylePlain target:self action:@selector(comfirm:)];
    _toolbar.items = @[cancelItem,emptyItem1,emptyItem2, comfirmItem];
    
}

- (void)cancel:(UIBarButtonItem *)sender{
    //  取消 视图消失
    [self.view endEditing:YES];
    
}
- (void)comfirm:(UIBarButtonItem *)sender{
    
    [self.view endEditing:YES];
    // 确认 选择日期格式 并 拿到日期
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSLog(@"%@",_datePicker.date);
    MYLog(@"%@",[formatter stringFromDate:_datePicker.date]);
  //  self.textField.text =[formatter stringFromDate:_datePicker.date];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];//定义一个NSCalendar对象
    
    NSDate *nowDate = [NSDate date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //生日
    NSDate *birthDay = _datePicker.date;//[dateFormatter dateFromString:birth];
    
    //用来得到详细的时差
    unsigned int unitFlags = kCFCalendarUnitYear | kCFCalendarUnitMonth | kCFCalendarUnitDay | kCFCalendarUnitHour | kCFCalendarUnitMinute | kCFCalendarUnitSecond;
    NSDateComponents *date = [calendar components:unitFlags fromDate:birthDay toDate:nowDate options:0];
    NSString *ageTemp;
    if([date year] >0)
    {
        NSLog(@"%@",[NSString stringWithFormat:(@"%ld岁%ld月%ld天"),(long)[date year],(long)[date month],(long)[date day]]) ;
        ageTemp = [NSString stringWithFormat:@"%ld岁",(long)[date year]];
    }
    else if([date month] >0)
    {
        NSLog(@"%@",[NSString stringWithFormat:(@"%ld月%ld天"),(long)[date month],(long)[date day]]);
        ageTemp = [NSString stringWithFormat:@"%ld个月",(long)[date month]];
    }
    else if([date day]>0){
        NSLog(@"%@",[NSString stringWithFormat:(@"%ld天"),(long)[date day]]);
        ageTemp = [NSString stringWithFormat:@"%ld天",(long)[date day]];
    }
    else {
        NSLog(@"0天");
        ageTemp = @"0岁";
    }
    //年龄更新
    CDOpenCountModel *ageModel = self.dataArray[1];
    ageModel.content = ageTemp;
    [self.dataArray replaceObjectAtIndex:1 withObject:ageModel];
    NSIndexPath *pathAge = [NSIndexPath indexPathForRow:1 inSection:0];
   
    //出生日期更新
    
    CDOpenCountModel *birthdayModel = self.dataArray[2];
    
    
    birthdayModel.content = [formatter stringFromDate:_datePicker.date];;
    [self.dataArray replaceObjectAtIndex:2 withObject:birthdayModel];
    NSIndexPath *pathBir = [NSIndexPath indexPathForRow:2 inSection:0];
    
    
    [self.tableView reloadRowsAtIndexPaths:@[pathAge,pathBir] withRowAnimation:UITableViewRowAnimationNone];
    
 
    
//    [self.textField endEditing:YES];
    
}

/** DatePicker */
- (void)createTheSixPicker
{
    // 选择性别
    _sixPicker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 200)];
    _sixPicker.dataSource = self;
    _sixPicker.delegate = self;
    
    _sixPicker.backgroundColor = [UIColor whiteColor];
    
    
    _sixToolbar = [[UIToolbar alloc]init];
    
    _sixToolbar.backgroundColor = [UIColor blackColor];
    
    _sixToolbar.barTintColor = kDefaultColor_sixthree;
    
    _sixToolbar.frame = CGRectMake(0, 0, KScreenWidth, 40);
    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelSix:)];
    
    UIBarButtonItem *emptyItem1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *emptyItem2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIBarButtonItem *comfirmItem = [[UIBarButtonItem alloc]initWithTitle:@"确认" style:UIBarButtonItemStylePlain target:self action:@selector(comfirmSix:)];
    _sixToolbar.items = @[cancelItem,emptyItem1,emptyItem2, comfirmItem];
    
}
- (void)cancelSix:(UIBarButtonItem *)sender{
    //  取消 视图消失
   [self.view endEditing:YES];
    
}
- (void)comfirmSix:(UIBarButtonItem *)sender{
    
    // 确认
    
    //年龄更新
    CDOpenCountModel *sixModel = self.dataArray[3];
    
   
    NSIndexPath *pathSix = [NSIndexPath indexPathForRow:3 inSection:0];
    
    if ([self.sixPicker selectedRowInComponent:0] == 0) {
        
        //self.textField.text = @"男";
        sixModel.content = @"男";
        [self.dataArray replaceObjectAtIndex:3 withObject:sixModel];
       
        [self.tableView reloadRowsAtIndexPaths:@[pathSix] withRowAnimation:UITableViewRowAnimationNone];
    }else{
       // self.textField.text = @"女";
        sixModel.content = @"女";
        [self.dataArray replaceObjectAtIndex:3 withObject:sixModel];
        
        [self.tableView reloadRowsAtIndexPaths:@[pathSix] withRowAnimation:UITableViewRowAnimationNone];
    }
    
    [self.view endEditing:YES];
    
}


****注意一个坑,cell复用的时候,键盘的样式也会复用

不用的时候添加一句

     cell.textField.inputView = nil;
    cell.textField.inputAccessoryView = nil;

0 0
原创粉丝点击