自定义单元格UITextField在所有部分的所有单元格中更新

时间:2015-05-17 06:44:13

标签: ios uitableview uicontrolevents

这是一个动态原型UITableView:在我的cellForRowAtIndexPath:

  1. 我已经取消了我的自定义单元格,这很好用:

    TimeSetViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"timeCell" forIndexPath:indexPath];
    
  2. 接下来我在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];
    
  3. 接下来我想通过一个事件来更新单元格的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];
    }
    
  4. 问题更新

    1. 我设法将startTimeTextendTimeText传递给cell.startTripTime.textcell.endTripTime.textsTimeTexteTimeText

    2. 我在[_tableView reloadData];中使用-(void)updateTextField:(UIDatePicker *)sender(在两个 if 块之后),数据显示在我的cell.startTripTime和{{1 UITextField,但它在我的各个部分中看起来都是一样的,而我希望它只更改第一部分的时间,如何我能解决这个问题?

      cell.endTripTime
    3. //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 _________________________ 似乎干扰并重新加载视图,而我甚至没有完成我想要的时间,如何我也能解决这个问题?

1 个答案:

答案 0 :(得分:0)

应该将action方法发送给其中一个选择器,startStripTime似乎是UIView实例而不是UIDatePicker实例。
是日期选择器更改其值,而不是包含它的视图 您还应该相应地更改更新方法。