简体   繁体   English

如何在iPhone中从datepickerview选择的单元格上调用日期

[英]how to call date on cell selected from datepickerview in iphone

hi friend i have to make date application i have groped tableview.i have one section and two row when my application start i am showing default date on both cell on first cell i was showing today day+1 and on nextcell i am showing today day+6 they i can see but 嗨,朋友,我必须进行日期应用程序,我摸索了tableview.i,当我的应用程序启动时,我有一个区域和两行。我在第一个单元格上显示了今天的默认日期。我能看到+6

- (id)init
{
    [super initWithStyle:UITableViewStyleGrouped];
    NSDate *todaysDate = [NSDate date]; 
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; 
    [dateComponents setDay:1]; 
    app.selectionData.fromDateSelected = [gregorian dateByAddingComponents:dateComponents toDate:todaysDate options:0]; 
    [dateComponents release]; 
    [gregorian release];

    //this is for 6day
    NSDate *todaysDate1 = [NSDate date]; 
    NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateComponents *dateComponents1 = [[NSDateComponents alloc] init]; 
    [dateComponents1 setDay:6]; 
    app.selectionData.toDateSelected = [gregorian1 dateByAddingComponents:dateComponents1 toDate:todaysDate1 options:0]; 
    [dateComponents1 release]; 
    [gregorian1 release];
   return self;

}

but know i want if 但知道我要不要
the first cell selected date is changed from date pickerview and second cell date is less than the first cell selected date then change the second cell date automatically to first cell selected date+5 please help me how to solve it what the change i have to do in my code wher i am show by default this for once time on this this is my code on run time this code i writemin my firstview controller class where i am show this date when my application start in tableviewcellrowAtindexpath 第一个单元格选择的日期已从日期pickerview中更改,第二个单元格的日期小于第一个单元格的选择日期,然后将第二个单元格的日期自动更改为第一个单元格的选择日期+5,请帮助我如何解决它我必须做的更改在我的代码中,我默认情况下显示一次,这是我在运行时的代码,这是我写的我的firstview控制器类,在我的应用程序在tableviewcellrowAtindexpath中启动时显示此日期

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    NSString*fromDate = [dateFormat stringFromDate:app.selectionData.fromDateSelected];
    // NSString *toDate = [dateFormat stringFromDate:targetDate1];
    NSString *toDate = [dateFormat stringFromDate:app.selectionData.toDateSelected];
    if ([indexPath row] == 0 && [indexPath section] == 1)
    {

        cell.textLabel.text=@"From Date";
        cell.detailTextLabel.text=fromDate;
        NSLog(@"this is for fromDate:%@",fromDate);
        }   
     if ([indexPath row] == 1 && [indexPath section] == 1)
    {

        cell.textLabel.text=@"To Date";
        cell.detailTextLabel.text=toDate;

        }


    [dateFormat release];
    dateFormat = nil;



this is code od dateview where i put my datepicker from here i selected date 


- (void)viewDidLoad
{
//  if(datePicker != nil && [datePicker retainCount] > 0)
//  { 
//      [datePicker release]; 
//      
//      datePicker = nil;
//} 
    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 0, 0)]; 
    [datePicker addTarget:self action:@selector(DateChangeForFinalPayMent) forControlEvents:UIControlEventValueChanged]; 
    datePicker.datePickerMode = UIDatePickerModeDate;  
    datePicker.date = [NSDate date];
    datePicker.minimumDate = [NSDate date];
    [self.view addSubview:datePicker];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.


}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

}
-(void)DateChangeForFinalPayMent{
    if ([selectionData type] == 0)
        [selectionData setFromDateSelected:[datePicker date]];

    else
        [selectionData setToDateSelected:[datePicker date]];

  //  [super viewWillDisappear:animated];



}

I wrote an example project for you that does this 我为您编写了一个示例项目

Basically - look at the delegate method of the picker view controller. 基本上-查看选择器视图控制器的委托方法。

It sends back a new date - and from that date you can calculate the second date and reload your table view. 它发送回一个新日期-从该日期起,您可以计算第二个日期并重新加载表格视图。

This is the code to get date from datepicker. 这是从datepicker获取日期的代码。 1st select the date & then select the cell you need to update. 首先选择日期,然后选择您需要更新的单元格。

 NSString *dateString = nil;

    -(void)DateChangeForFinalPayMent:(id)sender {

    NSLocale *usLocale = [[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"] autorelease];

    NSDate *pickerDate = [self.datePicker date];

    NSString *selectionString = [[NSString alloc] initWithFormat:@"%@",                              [pickerDate descriptionWithLocale:usLocale]];

        NSLog(@"string =%@",selectionString);

    dateString = selectionString;

        [selectionString release];
    }

On selecting the cell Perform this operation 选择单元格时执行此操作

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    selectedIndex = indexPath.row;
NSArray *array = [NSArray arrayWithObject:indexPath]; 

        [tablview reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

//some condition over here 

if(selectedIndex == indexPath.row)
cell.textLabel = dateString;

//some condition over her

}

You use this code according to your implementation.. 您可以根据自己的实现使用此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM