iphone – UITextField中的UIDatePicker在UITableView实时更新中
这个问题最近一直困扰着我.任何帮助,将不胜感激.
这些是我的目标: >在UITableViewCell中为UItextField设置UIDatePickerView. 到目前为止,我已经完成了所有这些,我可以看到选择器工作正常.每当我更改选择器值时,我都会使用NSLog来显示当前值,并且每次更改都能正常工作. 这就是我要的: 这是我的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 解决方法
您不需要实例化UIDatePicker的多个实例.人们应该这样做.将其分配给所有文本字段.现在要更新文本字段,您必须确定要更新的文本字段.为此,您应该采用UITextFieldDelegate协议并实现textFieldDidBeginEditing:方法来设置活动文本字段.现在,在UIDatePicker实例上更改值时,请更新活动文本字段.
但遗憾的是,这还不够.您将面临细胞重用的问题.要解决此问题,您必须为每个行维护一个日期数组,并在每次在UIDatePicker实例上更改值时更新数组.您可以通过标记文本字段或通过获取UITableViewCell实例的superview然后在UITableView上调用indexPathForCell:方法来识别文本字段所属的单元格. 并在旁注,return(label.text); [df发布];是错误的,因为该方法将在df释放之前返回.你应该在那里交换订单. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |