这个问题最近一直困扰着我,希望能得到帮助。
我的目标如下:
1. 为UITableViewCell中的UItextField设置UIDatePickerView。 我已经完成了这些工作,并且可以看到选择器正常工作。每当我更改选择器值时,我使用NSLog来显示当前值,并且对于每次更改都可以正常工作。
这就是我想要的: **每当我更改选择器值时,我需要UITextField(位于UITableViewCell中)的值也自动更改**
这是我的CellForRowIndexPath代码:
我下面还会发布其他代码。
我的目标如下:
1. 为UITableViewCell中的UItextField设置UIDatePickerView。 我已经完成了这些工作,并且可以看到选择器正常工作。每当我更改选择器值时,我使用NSLog来显示当前值,并且对于每次更改都可以正常工作。
这就是我想要的: **每当我更改选择器值时,我需要UITextField(位于UITableViewCell中)的值也自动更改**
这是我的CellForRowIndexPath代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.clearsOnBeginEditing = YES;
textField.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview:textField];
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(datePickerValueChangedd:) forControlEvents:UIControlEventValueChanged];
datePicker.tag = indexPath.row;
textField.inputView = datePicker;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
NSString *local;
local = [self datePickerValueChangedd: datePicker];
NSLog(@"%@", local); //DISPLAYS OUTPUT ONCE
textField.text =local;
[datePicker reloadInputViews];
[datePicker release];
// Configure the cell...
NSInteger row = [indexPath row];
cell.textLabel.text = [doseInfoDetailsArray objectAtIndex:row];
return cell;
}
我下面还会发布其他代码。
- (NSString *)datePickerValueChangedd:(UIDatePicker*) datePicker{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 68, 68)];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]];
NSLog(@"%@", label.text); // DISPLAYS OUTPUT WHENEVER PICKER IS MOVED
doseInfoDetailsTable.reloadData;
return (label.text);
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(t) name:TestNotification object:label];
[df release];
输出正确显示。但我需要它在UITextField中也发生变化。
2011-06-01 17:32:23.740 Tab[13857:207] Jun 1, 2017
2011-06-01 17:32:23.740 Tab[13857:207] Jun 1, 2017
2011-06-01 17:32:25.321 Tab[13857:207] Dec 1, 2017
2011-06-01 17:44:51.453 Tab[13857:207] Jun 1, 2017
2011-06-01 17:44:52.605 Tab[13857:207] Jun 1, 2018
2011-06-01 17:44:53.467 Tab[13857:207] Jun 2, 2018
2011-06-01 17:44:54.247 Tab[13857:207] Jun 5, 2018
textFieldDidBeginEditing:
方法来获取当前的UITextField
,但是我该如何从datePickerValueChanged
方法中更新该字段呢? - AngeloS- (void)textFieldDidBeginEditing:(UITextField *)textField { textField.text = 从UIDatePicker中获取的全局保存字符串; }
- AngeloSdatePicker
没有指向UIDatePicker
对象。请检查您在 IB 中是否正确设置或者是否在其他地方进行了更改。 - Deepak DanduproluUITextField
的值会调用datePickerValueChanged:
?如果有一个setTarget:action..
的东西,那就把它移除掉。 - Deepak DanduprolucellForRowAtIndexPath
中设置UITextField
时将setTarget:action..
设置为datePickerValueChanged
的事实... 对于我的疏忽我深表歉意。不过,我会发布一些示例代码,以防有人偶然遇到此问题。 - AngeloS