这是一个动态原型UITableView:在我的cellForRowAtIndexPath:
我已经取消了我的自定义单元格,这很好用:
TimeSetViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"timeCell" forIndexPath:indexPath];
接下来我在timeCell中将TimePicker设置为UITextField,这也可以正常工作(即在timeCell
中单击UITextField时显示时间选择器):
// Start Time
UIDatePicker *sTimePicker = [[UIDatePicker alloc]init];
sTimePicker.datePickerMode = UIDatePickerModeTime;
sTimePicker.backgroundColor = [UIColor whiteColor]
[cell.startTripTime setInputView:sTimePicker];
// End Time
UIDatePicker *eTimePicker = [[UIDatePicker alloc]init];
eTimePicker.datePickerMode = UIDatePickerModeTime;
eTimePicker.backgroundColor = [UIColor whiteColor];
[cell.endTripTime setInputView:eTimePicker];
接下来我想通过一个事件来更新单元格的UITextField,我从@Mani's post了解到,我已经设置了:
sTimePicker.tag = 1;
eTimePicker.tag = 2;
[sTimePicker addTarget:self action:@selector(updateTextField:)
forControlEvents:UIControlEventValueChanged];
[eTimePicker addTarget:self action:@selector(updateTextField:)
forControlEvents:UIControlEventValueChanged];
我的自定义方法-(void)updateTextField:(UIDatePicker *)sender
中包含了这个:
//Date Formatter
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setTimeStyle: NSDateFormatterShortStyle];
if(sender.tag == 1){
//Get Start Date
UIDatePicker *starttimepicker = (UIDatePicker*)sender.inputView;
thisStartTime = [starttimepicker date];
//Display Date
NSString *startTimeText = [dateFormat stringFromDate:thisStartTime];
sTimeText = [NSString stringWithFormat:@"%@",startTimeText];
}
if(sender.tag == 2){
//Get End Date
UIDatePicker *endtimepicker = (UIDatePicker*)sender.inputView;
thisEndTime = [endtimepicker date];
//Display Date
NSString *endTimeText = [dateFormat stringFromDate:thisEndTime];
eTimeText = [NSString stringWithFormat:@"%@",endTimeText];
}
我设法将startTimeText
和endTimeText
传递给cell.startTripTime.text
和cell.endTripTime.text
到sTimeText
和eTimeText
。
我在[_tableView reloadData];
中使用-(void)updateTextField:(UIDatePicker *)sender
(在两个 if 块之后),数据显示在我的cell.startTripTime
和{{1 UITextField,但它在我的各个部分中看起来都是一样的,而我希望它只更改第一部分的时间,如何我能解决这个问题?
cell.endTripTime
//I couldn't include the image but the output of the UITableView looks like this:
_________________________
Section 1
_________________________
Start Time : 4:50 PM
_________________________
End Time: 5:50 PM
_________________________
Section 2
_________________________
Start Time : 4:50 PM
_________________________
End Time: 5:50 PM
_________________________
似乎干扰并重新加载视图,而我甚至没有完成我想要的时间,如何我也能解决这个问题?
答案 0 :(得分:0)
应该将action方法发送给其中一个选择器,startStripTime似乎是UIView实例而不是UIDatePicker实例。
是日期选择器更改其值,而不是包含它的视图
您还应该相应地更改更新方法。