在這 我會給大家分享一個自己寫的小程序,詳細介紹它的使用...
如圖: 最上面的 其實是一行cell 左邊 起始時間 爲標題label ,右邊的22:37 爲副標題 label
功能,當點擊這行cell的時候,會從彈出如圖的這個一個日期選擇器,然後 選擇時間,再把這個時間設置給副標題顯示出來
該如何實現呢??????
下面就不給大家引申其他方法了,,直接呈上我的這個思路 及 代碼:
思路:
就是在當前cell中添加一個文本框 (不設置他的frame 這樣我們就看不到它了 )再讓他變成第一響應者 (因爲變成第一響應者之後 自然就會彈出鍵盤 ) 點擊工具欄上的按鈕時,再把鍵盤叫回去.然後把選擇的時間設置給cell
/日期選擇器
@property(nonatomic,strong)UIDatePicker *datePicker;
//工具欄
@property(nonatomic,strong)UIToolbar *toolBar;
//文本框
@property(nonatomic,strong)UITextField *textField;
但是默認彈出的鍵盤樣式 不是日期選擇器這種類型的.所以,我們可以通過兩句代碼,修改 文本框彈出的鍵盤類型 :
-(UITextField *)textField
{
if (_textField==nil) {
_textField=[[UITextField alloc]init];
//這行代碼可以指定文本框彈出鍵盤的樣式:
_textField.inputView = self.datePicker;
//這行代碼可以爲鍵盤指定一個工具欄
_textField.inputAccessoryView = self.toolBar;
}
return _textField;
}
---------------------------------------
-(UIToolbar *)toolBar
{
if (_toolBar==nil) {
_toolBar=[[UIToolbar alloc]init];
//工具欄的寬度是不用設置的,始終和屏幕一樣寬,然後緊貼鍵盤上面,所以只用指定一下工具欄的高度即可.
_toolBar.h=44; //這裏的.h是導入了UIView的擴展類..
_toolBar.barTintColor = [UIColor purpleColor];
// 創建工具欄中的三個按鈕
//取消按鈕
UIBarButtonItem *itemCancel=[[UIBarButtonItem alloc]initWithTitle:@"取消"style:UIBarButtonItemStylePlain target:self action:@selector(didClickCancelKeyboardButton) ];
// 彈簧 UIBarButtonSystemItemFlexibleSpace 可伸縮的空間
UIBarButtonItem *itemSpring = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// done
UIBarButtonItem *itemDone = [[UIBarButtonItem alloc] initWithTitle:@"完成"style:UIBarButtonItemStyleDone target:self action:@selector(didClickDoneButton)];
_toolBar.items = @[itemCancel, itemSpring, itemDone];
}
return _toolBar;
}
-----------------------------------------------
-(UIDatePicker *)datePicker
{
if (_datePicker==nil) {
_datePicker=[[UIDatePicker alloc]init];
//指定顯示的區域模式 傳一個國家的代號
_datePicker.locale=[NSLocale localeWithLocaleIdentifier:@"zh-Hans"];
//指定一個顯示的模式:我們只用到時間
_datePicker.datePickerMode=UIDatePickerModeTime;
// UIDatePickerModeTime, 時間 // Displays hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. 6 | 53 | PM)
// UIDatePickerModeDate, 日期 // Displays month, day, and year depending on the locale setting (e.g. November | 15 | 2007)
// UIDatePickerModeDateAndTime, 日期和時間 // Displays date, hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM)
// UIDatePickerModeCountDownTimer,
}
return _datePicker;
}
---------------------------------------------------------
/ 選中某個cell後會執行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// 記錄indexPath
self.idxPath = indexPath;
// 如果點擊的是第0組, 那麼什麼都不做
if (indexPath.section == 0) return;
// 如果點擊的不是第0組, 那麼再彈出鍵盤
// 1. 創建文本框, 並且把文本框添加到當前的cell中, 同時讓當前文本框變成第一響應者
FDUITbaleViewCell *cell = (FDUITableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath]; //這裏用的是自定義cell 如上圖中所示的cell的樣式,那麼具體使用那種cell的樣式,我們下面對有圖示
[cell.contentView addSubview:self.textField];
[self.textField becomeFirstResponder];
}
----------------------------------------------------------------------------------------------
//點擊了工具欄上的取消按鈕,然後把鍵盤叫回去
-(void)didClickCancelKeyboardButton
{
//辭去第一響應者
[self.view endEditing:YES];
//移除單元格 避免了 每次點擊單元格的時候 一個文本框 添加多次....
[self.textFiled removeFromSuperview];
}
--------------------------------
// 點擊完成按鈕
- (void)didClickDoneButton {
// 1. 把鍵盤叫回去
[self.view endEditing:YES];
// 2. 把用戶選擇的時間設置給detailTextLabel
// 2.1 獲取用戶選擇的時間
NSDate *date = self.datePicker.date;
// 2.2 把用戶選擇的時間轉換爲字符串類型
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"HH:mm";
NSString *strTime = [formatter stringFromDate:date];
// 2.3 把時間設置給當前選中的cell的detaisTextLabel
// 2.3.1 獲取當前選中的cell
FDUITbaleViewCelll *cell = [self.tableView cellForRowAtIndexPath:self.idxPath];
// cell.detailTextLabel.text = strTime;
[cell setTimeWithText:strTime]; //setTimeWithText:strTime中實現了對這句代碼的封裝
// 3. 把文本框從父容器中移除
[self.textFiled removeFromSuperview];
}