2014-04-04 58 views
0

我是新來的ios開發,但我做的是這樣的:我有一個表格視圖,我實現了顯示日期選擇器,當第一個單元格點擊表格視圖[日期選擇器將顯示在第一個單元格下方]。所以現在我想根據日期選擇器中選擇的日期重新加載單元格,但是整個表格視圖不應該重新加載,因爲允許顯示日期選擇器的第一個單元格應該始終存在。如何加載單元格而不影響iOS中表格視圖的第一個單元格

所以我想加載一些數據的單元格而不影響第一個單元格。我怎樣才能做到這一點。我有這樣一個想法:

如果我可以識別除第一個單元格之外的剩餘單元格,那麼我可以在我的cellForRowAtIndexpath方法中使用if條件,但我不知道如何識別剩餘的單元格..請別人幫助我。我將把我的代碼如下..

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


    UITableViewCell *cell; 

    NSDate *today = [NSDate date]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    // display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings 
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
    NSString *currentTime = [dateFormatter stringFromDate:today]; 
    NSDate *date=[dateFormatter dateFromString:currentTime]; 



    if(indexPath.row==0){ 
     VCPerson *person = self.persons[indexPath.row]; 

     cell = [self createPersonCell:person]; 

    } 

    else if ([self datePickerIsShown] && (self.datePickerIndexPath.row == indexPath.row)){ 

     // VCPerson *person = self.persons[indexPath.row -1]; 



     cell = [self createPickerCell:date]; 

    } 




    if(indexPath.section!=0 && tapfirstCell) { 

     UITableViewCell *cell = (UITableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:kOtherCellIdentifier forIndexPath:indexPath]; 
     //cell.delegate_Dtepick = self; 
     NSDictionary *dictionary = [_dataArray objectAtIndex:indexPath.section]; 
     NSArray *array = [dictionary objectForKey:@"data"]; 
     NSString *cellValue = [array objectAtIndex:indexPath.row]; 
     cell.textLabel.text =cellValue; 

    } 

    return cell; 




    /* 
    if ([self datePickerIsShown] && (self.datePickerIndexPath.row == indexPath.row)){ 

     // VCPerson *person = self.persons[indexPath.row -1]; 



     cell = [self createPickerCell:date]; 

    } 



    else { 

     // cellForDatePickCell* cell; 
     VCPerson *person = self.persons[indexPath.row]; 

     cell = [self createPersonCell:person]; 
     //cell = [self createCustomCell:indexPath]; 
    } 

    return cell; 


*/ 




    } 

在我的代碼它不顯示第一個單元格後,剩餘的細胞作爲我想要的。請有人幫助我。

編輯: 這裏是我的整個類

http://pastie.org/8993858

編輯2:

這裏是什麼,我需要做一個說明..

enter image description here

+0

你能顯示更多的代碼嗎?例如,' - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {'因爲它決定了要創建多少個單元格。 –

+1

@BenB c我編輯過的帖子兄弟..謝謝你試圖幫我 – Darshana

+0

如果條件失敗會發生什麼? 'if([self datePickerIsShown] &&(self.datePickerIndexPath.row == indexPath.row))' – abmussani

回答

2

你的DatePicker應該在HeaderView中而不是在TableViewCell中。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    if (headerView == nil) 
     headerView = [[[HeaderView alloc] init] autorelease]; // further customizations 
    return headerView; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    return 30.0; 
} 

創建headerView包含DatePicker或任何想要添加到您的HeaderView的視圖。在重新加載tableView時,您可以簡單地返回已經創建的headerView,並根據需要更新所有viewAtRows。 This教程可以幫助您瞭解如何自定義HeaderView。

+0

謝謝..我想這會幫助我..我會嘗試讓你知道:) – Darshana

+0

這裏我有一個quiestion @Faisal在我的情況下,當我觸摸第一個單元格,然後日期選擇器向下滑動單元格下方。我可以在headerView中做同樣的事情嗎? – Darshana

+0

我在說這樣的事情:http://masteringios.com/blog/2013/10/31/ios-7-in-line-uidatepicker/ – Darshana

0

我剛剛有一個想法。你應該刪除選擇器行,然後插入你想要的行。使用以下兩種方法實現:

首先,調用[self.tableView deleteRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimation]可以很明顯地刪除選擇器行。

然後在那之後致電[self.tableView insertRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimation],讓表知道。

當然,這不是所有的工作,你必須正確處理UITableView數據源以符合這種情況(numberOfRowsAtIndexPath,cellForRowAtIndexPath ...應該返回適​​當的結果)。

全部在這裏:Apple Docs

+0

中所述現在..我可以實現日期選擇器功能成功......但是當選擇日期我想重新加載單元格..但也選擇器單元格也應該在那裏,因爲我在我的帖子顯示..我認爲解決方案你在這裏給出的,刪除picker單元格,並將加載其他單元格..但我希望picker單元格也與其他單元格 – Darshana

+0

@Darshana:所以你不刪除日期選擇器單元格,只需插入新行。 :) – Envil

相關問題