2014-07-17 40 views
-1

我有唯一部分(說)五行的tableview。編輯自定義單元格的代碼都可以工作,但是當我需要用包含較少行的新數據重新加載表格數據時(例如兩次),就會出現問題。如果我編輯的索引高於重載表中的最大行數,那麼程序會崩潰。異常表明它試圖訪問超出新錶行數限制的更高編號的元素。UITableView以編程方式選擇行沒有預期的效果

我試圖把:

[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop]; 

在之前的

[self.tableView reloadData]; 

,但它不工作。

+0

需要更多的解釋,如果應用程序崩潰把錯誤日誌。 –

+0

如果您的編輯單元操作起作用,則不表示它正確完成。我認爲你應該在這裏粘貼代碼,你改變你的表視圖數據,表視圖數據源和表視圖委託方法 – Szu

+0

我們需要所有的tableView委託方法,請寫下來 –

回答

0

到底,取消選擇舊的行(即舊數據源仍然是當前的),然後用新的dat選擇第一行一個來源解決了問題。

0

它只能意味着你在這裏訪問的第一行:

[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop]; 

,當你不具備的第一行。

當您重新加載tableview時,請確保您至少有一行。

試着這樣做:

if([self.tableview numberOfRowsInSection:0]>0) 
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop]; 
+0

(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section – Nick

0

我相信你用下面的代碼來取消選擇你的​​

[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop]; 

如果是這樣,你應該用這個代替

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
[self.tableView deselectRowAtIndexPath:indexPath animated:NO]; 
相關問題