我有此代碼將一行添加到表格視圖中我的應用程序的根視圖控制器:獲得「索引0超出界限爲空數組」非空數組錯誤
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray *myArray = [NSArray arrayWithObject:indexPath];
NSLog(@"count:%d", [myArray count]);
[self.tableView insertRowsAtIndexPaths:myArray withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
當它是與模擬器中運行,我得到這樣的輸出:
數:1
*終止應用程序由於未捕獲的異常 'NSRangeException',原因是:「* - [NSMutableArray的objectAtIndex:]:索引0超越界限空陣列'
這是發生在[self.tableView insertRowsAtIndexPaths:myArray withRowAnimation:UITableViewRowAnimationFade];
線,但NSLog
聲明顯示,被稱爲myArray
的NSArray
不是空的。我錯過了什麼嗎?有沒有人曾經遇到過這個?
在所有委託/數據源方法上放置斷點,然後遍歷insertRowsAtIndexPaths行。看看你最終在哪裏,並試圖精確地縮小拋出異常的位置。 –