2009-11-12 50 views
9

我有一個分組表格視圖,我想重新加載一個特定的行。當我調用reloadRowsAtIndexPaths時:行從表視圖中完全消失。還有什麼我需要做的?我誤解了reloadRowsAtIndexPaths:

//this is the method doing the reload 
-(void)setDurationTableViewCell:(NSString *)dur { 

    self.workoutDuration = dur; 
    NSIndexPath *durPath = [NSIndexPath indexPathForRow:3 inSection:0]; 
    NSArray *paths = [NSArray arrayWithObject:durPath]; 
    [woTableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationRight]; 

}//end setDurationTableViewCell 


//this is the cellForRowAtIndexPath method if it has anything to do with my issue 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell; 

    if (indexPath.row == 0) { 

     //workout comments 
     cell = [tableView dequeueReusableCellWithIdentifier:@"workoutCommentsCell"]; 
     if (nil == cell) { 
      cell = workoutCommentsCell; 
      cell.selectionStyle = UITableViewCellStyleValue1; 
     } 

    }else if (indexPath.row == 1) { 

     //difficulty 
     cell = [tableView dequeueReusableCellWithIdentifier:@"workoutDifficultyCell"]; 
     if (nil == cell) { 
      cell = workoutDifficultyCell; 
      cell.selectionStyle = UITableViewCellStyleValue1; 
     } 
     cell.textLabel.text = [NSString stringWithFormat:@"Difficulty: %@", self.workoutDifficulty]; 

    }else if (indexPath.row == 2) { 

     //workoutDate 
     cell = [tableView dequeueReusableCellWithIdentifier:@"workoutDateCell"]; 
     if (nil == cell) { 
      cell = workoutDateCell; 
      cell.selectionStyle = UITableViewCellStyleValue1; 
     } 
     cell.textLabel.text = [NSString stringWithFormat:@"Date: %@", self.workoutDate]; 

    }else if (indexPath.row == 3) { 

     //workoutDuration 
     cell = [tableView dequeueReusableCellWithIdentifier:@"workoutTimerCell"]; 
     if (nil == cell) { 
      cell = workoutTimeCell; 
      cell.selectionStyle = UITableViewCellStyleValue1; 
     } 
     cell.textLabel.text = [NSString stringWithFormat:@"Duration: %@", self.workoutDuration]; 

    }//end else-if 


    return cell; 

}//end cellForRowAtIndexPath 
+0

你有沒有得到這個解決?我有同樣的問題。 – MikeyWard 2010-08-20 01:43:05

回答

10

嘗試包裹裏面

[self.tableView beginUpdates]; 
[self.tableView endUpdates]; 

如您ReloadRowsAtIndexPaths

[self.tableView beginUpdates]; 
[woTableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationRight]; 
[self.tableView endUpdates] 
+0

什麼幫助我完全刪除reloadRowsAtIndexPaths:只剩下 [self.tableView beginUpdates]; [self.tableView endUpdates]; – Krizai 2016-03-25 17:00:45

+0

僅供參考。我嘗試在一個'beginUpdates' endUpdates塊中包裝幾個'reloadRowsAtIndexPaths',但是這導致了'accessoryView'的一個尷尬行爲,'accessoryView'是一個存儲在屬性中的'UISwitch'。當閱讀文檔begin/endUpdates用於插入,刪除和選擇。 – 2018-01-10 10:29:34

5

我最近開始學習iPhone SDK和最有可能我不能給你答案......但是,難道你不具有以替換if (nil == cell)塊代碼

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
reuseIdentifier:@"workoutTimerCell"] autorelease]; 
+1

+1我同意@CodeSeavers。它看起來像你沒有分配任何單元格。即使你正在爲'workoutDifficultyCell'分配一些東西,你也需要爲表格中的其他單元分配更多的東西。我建議您在Apple網站上查看TableViewSuite以瞭解更多信息。 – AWrightIV 2011-06-14 18:14:32

3

我有同樣的問題,顯然設置withRowAnimation:UITableViewRowAnimationNone爲我解決它。 不能說爲什麼,但否則細胞製作一個不錯的動畫,然後消失。

3
dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
    });