2013-11-15 52 views
10

我在UITableView中添加或刪除行的動畫添加或刪除時存在問題,該行的高度與其他行的高度不同。將大行插入UITableView時的動畫假設高度錯誤

下面的gif演示了插入和刪除默認高度(44pts)和更大的行(100pts)的行的問題。左邊的一個是模擬器的屏幕錄製(新的單元格覆蓋第五行是一個不同的問題),右邊的單元格是它應該做什麼的模型。

gif of animation issuegif of how it should look

在我的情況,我有一堆行的,身高每60pts。當單元格中的按鈕被輕敲時,「編輯」單元格將從下面滑出,向下推動較低的單元格。這個編輯單元是180pts高。當我呼叫insertRowsAtIndexPaths:withRowAnimation:deleteRowsAtIndexPaths:withRowAnimation:時,動畫假設錯誤的高度爲60pts,而不是180pts,它應該是
這意味着在UITableViewRowAnimationTop的情況下,新單元格出現在距離它最終位置的-60pts處,並且下滑到新的位置;約三分之一的動畫應該在做。同時,下面的行從起始位置平滑地向下移動180點,完全如其。

有沒有人制定出實際的解決方案呢?某種方式來告訴新的行它應該是什麼動畫?

下面是我用來隱藏和顯示編輯行的代碼。我使用的是TLSwipeForOptionsCell觸發編輯,但它很容易使用例如tableView:didSelectRowAtIndexPath:

-(void)hideEditFields{ 
    [self.tableView beginUpdates]; 

    [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:editFormVisibleForRow+1 inSection:0]] withRowAnimation:UITableViewRowAnimationTop]; 
    editFormVisibleForRow = -1; 

    [self.tableView endUpdates]; 
} 

-(void)cellDidSelectMore:(TLSwipeForOptionsCell *)cell{ 
    NSIndexPath* indexPath = [self.tableView indexPathForCell:cell]; 
    // do nothing if this is the currently selected row 
    if(editFormVisibleForRow != indexPath.row){ 
     if(editFormVisibleForRow >= 0){ 
      [self hideEditFields]; 
      // update the index path, as the cell positions (may) have changed 
      indexPath = [self.tableView indexPathForCell:cell]; 
     } 

     [self.tableView beginUpdates]; 

     editFormVisibleForRow = indexPath.row; 
     [self.tableView insertRowsAtIndexPaths:@[ 
              [NSIndexPath indexPathForItem:editFormVisibleForRow+1 inSection:0] 
              ] withRowAnimation:UITableViewRowAnimationTop]; 

     [self.tableView endUpdates]; 
    } 
} 

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return _dataSource.count + (editFormVisibleForRow >= 0 ? 1 : 0); 
} 

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    int row = indexPath.row; 
    if(editFormVisibleForRow >= 0 && row > editFormVisibleForRow && row <= editFormVisibleForRow + 1){ 
     return 180.0f; 
    } 
    else return 60.0; 
} 

了一下週圍戳複製,看起來這是一個沒有明確答案的一個常見問題。我在這裏找到的大多數類似問題都沒有答案,或提供特定於提問者情況的解決方法。 (例子:Problem with RowAnimationCustom UITableViewCell height yields improper animation,UITableView animation glitch when deleting and inserting cells with varying heights)。此外,我試圖製作三個較小的行並對它們進行動畫處理,而不是試圖製作一個三倍大小的編輯行,但這並不合適,因爲它們全都出現。我也嘗試過一個接一個地動畫它們,但放鬆使它看起來很奇怪,發生了明顯的3步動畫,而不是整個編輯視圖在一個動作中滑出視圖。

編輯:我剛剛注意到,如果我調用reloadRowsAtIndexPaths:withRowAnimation: UITableViewRowAnimationNone上面的那一行,我嘗試動畫,它會改變動畫的行爲;即動畫假設高度爲0pts,如以下動畫所示。它更接近我想要的東西,但還是不對的,作爲動畫的速度是錯誤的,它留下的間隙(在我的應用程序,這意味着背景 顏色戳透)

gif of issue with previous row reloaded

+0

你想通了嗎?試圖在這裏做類似的事情...... thx – minovsky

+0

不幸的是客戶改變了我們的設計,這個功能不再需要,所以我從來沒有解決它。如果我再次遇到它並解決它,我一定會回答問題 –

+1

同樣的問題:( – OldFox

回答

-1

此,使用didSelectRowAtIndexPath方法工作對我來說:

@interface TableController() 
@property (strong,nonatomic) NSArray *theData; 
@property (strong,nonatomic) NSIndexPath *pathToEditCell; 
@end 

@implementation TableController 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.theData = @[@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine"]; 
    [self.tableView reloadData]; 
} 


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return ([indexPath isEqual:self.pathToEditCell])? 100: 44; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return (self.pathToEditCell == nil)? self.theData.count: self.theData.count + 1; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if ([indexPath isEqual:self.pathToEditCell]) { 
     RDEditCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditCell" forIndexPath:indexPath]; 
     return cell; 
    }else{ 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
     cell.textLabel.text = self.theData[indexPath.row]; 
     return cell; 
    } 
} 


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

    if (self.pathToEditCell == nil) { // first time selecting a row 
     self.pathToEditCell = [NSIndexPath indexPathForRow:indexPath.row +1 inSection:indexPath.section]; 
     [self.tableView insertRowsAtIndexPaths:@[self.pathToEditCell] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    }else if ([self.pathToEditCell isEqual:indexPath]){ // deletes the edit cell if you click on it 
     self.pathToEditCell = nil; 
     [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

    }else{ // close the old edit cell and adds another if you click on another cell while the edit cell is on screen 
     [self.tableView beginUpdates]; 
     [self.tableView deleteRowsAtIndexPaths:@[self.pathToEditCell] withRowAnimation:UITableViewRowAnimationFade]; 
     self.pathToEditCell = indexPath; 
     [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [self.tableView endUpdates]; 
    } 
} 

對於缺失,我喜歡動畫的「變臉」選項的外觀,而是「頂」也是好的。

+1

你的代碼有同樣的問題,編輯單元只從-44pts滑出,而不是100pts,這使得單元看起來像是從中心擴展而不是滑下來。 ://i34.photobucket.com/albums/d135/death_au/cell-anim-issue.gif –

+0

@death_au,好吧,我想我誤解了你的問題 - 我以爲你只是看到了新單元的一部分,就個人而言,我沒有看到那個動畫有什麼問題 - 它對我來說看起來不錯 - 我甚至沒有注意到你在說什麼,直到我放慢了速度。無論如何,我不知道解決這個問題的方法。 – rdelmar