![](/img/trans.png)
[英]Crash on UITableView endUpdates when moving last row in section
[英]UITableView endUpdates crash when called in viewDidLoad
我有一個UITableView,我想在我的視圖控制器的viewDidLoad中對其進行修改。 但是UITableView不會在viewDidLoad中查詢其dataSource,因此我先調用reloadData,然后使用beginUpdates和endUpdates修改其單元格。 (就我而言,我只是刪除一些單元格)。 但是,調用endUpdates時會引發異常:
[__NSArrayM insertObject:atIndex:]:索引1超出了空數組的范圍。
這個例外是什么意思? 我知道我正在向UITableView返回正確的數字,因為如果我在viewDidAppear之后調用它,它會完美工作,但在viewDidLoad中就無法使用。
編輯:
這是我的代碼
for (int i = 0; i < [customDataSource numberOfSectionsInTableView]; i ++) {
NSInteger n = [customDataSource tableView:self numberOfRowsInSection:section];
NSMutableArray* array = [[NSMutableArray alloc] initWithCapacity:n];
for (NSInteger i = 0; i < n; i++)
[array addObject:[NSIndexPath indexPathForRow:i inSection:section]];
[super beginUpdates];
[super deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];
[super endUpdates];
}
當我將此代碼從viewDidAppear移到viewDidLoad或viewWillAppear時,它將崩潰。
似乎您正在嘗試將索引1的對象插入空數組。 如果數組為空,則插入的唯一可接受索引為0。檢查如何計算索引值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.