Leg*_*las 5 iphone xcode objective-c uidatepicker uipickerview
这个问题最近一直困扰着我.任何帮助,将不胜感激.
这些是我的目标:
到目前为止,我已经完成了所有这些,我可以看到选择器工作正常.每当我更改选择器值时,我都会使用NSLog来显示当前值,并且每次更改都能正常工作.
这就是我想要的东西: **每当我改变pickervalue,我需要的价值,也可以在的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;
}
Run Code Online (Sandbox Code Playgroud)
我也在下面发布其他代码.
- (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];
Run Code Online (Sandbox Code Playgroud)
}
输出正确显示.但我还需要在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
Run Code Online (Sandbox Code Playgroud)
您不需要实例化多个实例UIDatePicker.人们应该这样做.将其分配给所有文本字段.现在要更新文本字段,您必须确定要更新的文本字段.为此,您应该采用UITextFieldDelegate协议并实现该textFieldDidBeginEditing:方法来设置活动文本字段.现在,在UIDatePicker实例上更改值时,更新活动文本字段.
但遗憾的是,这还不够.您将面临细胞重用的问题.要解决此问题,您必须为每个行维护一个日期数组,并在每次在UIDatePicker实例上更改值时更新数组.您可以通过tagging它或通过获取将成为UITableViewCell实例的superview 然后调用indexPathForCell:方法来识别文本字段所属的单元格UITableView.
而在一个侧面说明,return (label.text); [df release];是错误的,因为该方法将之前返回df为released.你应该在那里交换订单.
| 归档时间: |
|
| 查看次数: |
8851 次 |
| 最近记录: |