我在UITableView中添加或刪除行的動畫添加或刪除時存在問題,該行的高度與其他行的高度不同。將大行插入UITableView時的動畫假設高度錯誤
下面的gif演示了插入和刪除默認高度(44pts)和更大的行(100pts)的行的問題。左邊的一個是模擬器的屏幕錄製(新的單元格覆蓋第五行是一個不同的問題),右邊的單元格是它應該做什麼的模型。
在我的情況,我有一堆行的,身高每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 RowAnimation,Custom UITableViewCell height yields improper animation,UITableView animation glitch when deleting and inserting cells with varying heights)。此外,我試圖製作三個較小的行並對它們進行動畫處理,而不是試圖製作一個三倍大小的編輯行,但這並不合適,因爲它們全都出現。我也嘗試過一個接一個地動畫它們,但放鬆使它看起來很奇怪,發生了明顯的3步動畫,而不是整個編輯視圖在一個動作中滑出視圖。
編輯:我剛剛注意到,如果我調用reloadRowsAtIndexPaths:withRowAnimation: UITableViewRowAnimationNone
上面的那一行,我嘗試動畫,它會改變動畫的行爲;即動畫假設高度爲0pts,如以下動畫所示。它更接近我想要的東西,但還是不對的,作爲動畫的速度是錯誤的,它留下的間隙(在我的應用程序,這意味着背景 顏色戳透)
你想通了嗎?試圖在這裏做類似的事情...... thx – minovsky
不幸的是客戶改變了我們的設計,這個功能不再需要,所以我從來沒有解決它。如果我再次遇到它並解決它,我一定會回答問題 –
同樣的問題:( – OldFox