[英]UITableView exception when scroll and add cell
我遇到了這個問題,我已經在整個互聯網上進行搜索,但仍然找不到解決方案。 請看下面的代碼:
NSLog(@"%i", [self tableView:tableView numberOfRowsInSection:0]); // 4
[self updateCardIDArray];
NSLog(@"%i", [self tableView:tableView numberOfRowsInSection:0]); // 5
int indexOfCreatedCard = [cardIDs indexOfObject:newCardID];
NSLog(@"%i", indexOfCreatedCard); // 4
[tableView reloadData];
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexOfCreatedCard inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:FALSE];
NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:[NSIndexPath indexPathForRow:indexOfCreatedCard inSection:0], nil];
[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
基本上,我想要在這里實現的是將表滾動到剛創建的單元格,然后顯示其添加的動畫。使用此代碼時,出現以下錯誤:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:1070
和這個例外:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.
關鍵是,如果我刪除滾動條和重新加載數據行,則程序將正常運行(當然,無法看到添加的單元格動畫)。 任何幫助將不勝感激!
嘗試
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexOfCreatedCard inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:NO];
我認為問題是,在插入單元格之前您正在滾動,我只是發布了適合您問題的示例代碼
- (IBAction)scrollPlease:(id)sender
{
aTableView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0); //to move some position below
k = k + 1; //hear k is the row numbers in ur case "indexOfCreatedCard"
[aTableView beginUpdates]; // first u add the cell to tableview
[aTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:k-1 inSection:0]] withRowAnimation:UITableViewRowAnimationMiddle];
[aTableView endUpdates]; //hear (k - 1) is the last row
[aTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:k-1 inSection:0] atScrollPosition:UITableViewRowAnimationTop animated:NO]; //then scroll to last cell it will show the animation of adding the cell
aTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); // after u set it to zero
}
希望你有一些主意:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.