开发者

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

- (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 writemi开发者_运维技巧n my firstview controller class where i am show this date when my application start in 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. 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..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜