2011-09-27 50 views
0

喜的朋友,我必須讓時間申請 細胞迄今爲止我已經摸索tableview.i有一個部分和兩個排 當我的應用程序開始我顯示的第一個細胞都在默認的日期電池我顯示今天白天+ 1和nextcell我今天顯示當天+ 6他們我可以看到,但如何調用從datepickerview選擇了iphone

- (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; 

} 

,但知道我想如果
第一個單元格選定的日期是從日期pickerview和第二小區變更日期小於第一個單元格選擇日期,然後將第二個單元格日期自動更改爲第一個單元格選定日期+ 5請幫助我如何解決我t什麼是我的代碼我必須做的改變我是默認顯示這一次在這個這是我的代碼在運行時間 這段代碼我writemin我的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) 
    { 

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

     [email protected]"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]; 



} 
+0

你的問題不明確。你想實現什麼 – Anand

+0

@Anand我想實現第一個單元格選擇日期從日期選擇器視圖改變,第二個單元格日期小於第一個單元格選擇日期,然後將第二個單元格日期自動更改爲第一個單元格選定日期+ 5 – Rocky

+0

好的,我開始知道,當你選擇第一個單元格時,你想更新單元格右邊的日期。等待幾分鐘,我將發佈代碼 – Anand

回答

1

我寫了example project爲你做這個

基本上 - 看看選擇器視圖控制器的委託方法。

它發回一個新的日期 - 從那天起,您可以計算第二個日期並重新載入您的表格視圖。

-1

這是從datepicker獲取日期的代碼。首先選擇日期&然後選擇您需要更新的單元格。

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]; 
    } 

在選擇單元格執行此操作

- (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 

} 

您可以根據實現使用此代碼..

+0

您的內存管理被破壞了...... – bbum

+0

@bbum感謝您瞭解我的錯誤。 – Anand