2011-10-19 52 views
10

我有幾個NSMutableArrays,我需要清除刷新視圖時。但是,當我試圖用[array removeAllObjects];清除它們時,由於index beyond bounds error,我的tableview崩潰。我所做的所有刷新都清除了數組,並調用與viewDidLoad中相同的函數來填充tableview。 [tableView reloadData]直到方法的最後一行纔會被調用。清除NSMutableArray刷新

編輯:這很可能是這個問題:我使用拉來刷新外部庫,並且當你向上滾動並釋放表時,它反彈回來,因此UITableView嘗試加載下一個單元格,它它不能因爲數組被清除,並且它仍然被加載。

答案:從數組中removeAllObjects,立即做一個self.tableView reloadData,然後繼續其餘的。

+0

向我們展示你的代碼 – Nekto

回答

38

問題可能是由於numberOfRowsInSection返回一些計數而您的數據源數組爲空。

只是調用[array removeAllObjects]numberOfRowsInSectionreturn [array count]

我希望它能解決您的問題。運氣最好!

+0

感謝很多人:) – Supertecnoboff

+0

是removeAllObjects釋放等於該對象? –

0

刷新陣列時,首先檢查它是否有對象&然後重新初始化陣列並釋放一個。

3

我刪除我的表格視圖單元以下述方式

NSMutableArray* indexPathsToDelete = [[NSMutableArray alloc] init]; 
      for(unsigned int i = 0; i < [self.array count]; i++) 
      { 
       [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:0]]; 
      } 

[self.tableView beginUpdates]; 
[self.array removeAllObjects]; 
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationLeft]; 
[self.tableView endUpdates]; 
[indexPathsToDelete release];