2012-07-29 89 views
35

我正在使用UITableViewController並在更新tableView時出現此錯誤。下面是我的代碼:NSInternalInconsistencyException',原因:'試圖插入第0行到第0部分,但更新後第0部分只有0行'

出現這種情況時,我做一個click事件:

[timeZoneNames insertObject:@"HELLO" atIndex:0]; 
[self.tableView beginUpdates]; 
NSArray *insertIndexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; 
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop]; 
[self.tableView endUpdates]; 

我試圖尋找蘋果的文檔但didnt幫助。

感謝, 阿比

回答

27

我以前就遇到了這個問題。這意味着,當-insertRowsAtIndexPaths:withRowAnimation:被稱爲-tableView:numberOfRowsInSection:返回0。你需要插入到你的模型調用-insertRowsAtIndexPaths:withRowAnimation:之前(見:-beginUpdates


更新

我不知道的-tableView:numberOfRowsInSection:返回值是?另外,如果您只有一個更新,則不需要-beginUpdates/-endUpdates

[timeZoneNames insertObject:@"HELLO" atIndex:0]; 
// Let's see what the tableView claims is the the number of rows. 
NSLog(@"numberOfRowsInSection: %d", [self tableView:self.tableView numberOfRowsInSection:0]); 
NSArray *insertIndexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; 
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop]; 
+0

我已經做到了.. [timeZoneNames insertObject:@「HELLO」atIndex:0]; 行數總是大於1.仍然收到此錯誤.. – insomiac 2012-07-29 19:18:55

+0

返回類型numberOfRowsInSection爲[timeZoneNames count]; 使用insertRowsAtIndexPaths沒有beginUpdates和endUpdates是好的嗎? – insomiac 2012-07-30 03:16:06

+0

@Aby從'-beginUpdates'文檔:「如果您希望後續插入,刪除和選擇操作(例如,cellForRowAtIndexPath:和indexPathsForVisibleRows)同時進行動畫,請調用此方法。」它用於將多個UI操作分組爲單個UI操作:只有一個操作。 – 2012-07-30 10:55:55

4

我試着打印出多少行和節這讓我想,有多少部分我會在表視圖數據源方法返回在更新過程中實際上是在分別的tableView ...這是罪魁禍首:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 0; 
} 

確保你正在返回1和0不

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

至少這解決了這個問題對我來說...

UPDATE:

我再有同樣的問題這是工作一段時間後

。我修改了一下應用程序。我的第一個視圖是一個tableView,當你點擊右上角的加號按鈕時,你會得到另一個表格,你可以輸入信息。當您單擊完成後,將信息添加到與此代碼的核心數據模型:

- (IBAction)doneButtonPressed:(UIBarButtonItem *)sender 
{ 

MoneyBadgeAppDelegate *appDelegate = 
[[UIApplication sharedApplication] delegate]; 

NSManagedObjectContext *context = 
[appDelegate managedObjectContext]; 
NSManagedObject *newMoment; 
newMoment = [NSEntityDescription 
      insertNewObjectForEntityForName:@"SpendingMoment" 
      inManagedObjectContext:context]; 
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"yyyyMMdd"]; 
NSDate *modifiedDate = [dateFormat dateFromString: self.dateTextField.text]; 

[newMoment setValue: modifiedDate forKey:@"date"]; 
[newMoment setValue: descTextField.text forKey:@"desc"]; 

[appDelegate.eventsArray insertObject:newMoment atIndex:0]; 

NSError *error; 
[context save:&error]; 

[self dismissViewControllerAnimated:YES completion:nil]; 

} 

我證實,它被通過打印到控制檯回國後在我的viewDidAppear方法如下放入核心數據模型成功地回到第一個屏幕。

-(void)viewDidAppear:(BOOL)animated{ 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SpendingMoment" 
inManagedObjectContext:self.managedObjectContext]; 
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
[fetchRequest setEntity:entity]; 

NSError *error; 
NSArray *items = [self.managedObjectContext 
        executeFetchRequest:fetchRequest error:&error]; 

for (SpendingMoment *moment in items) { 
    NSLog(@"Date: %@, Desc: %@", [moment date], [moment desc]); 
} 

[self addEvent]; 

然後我的addEvent方法做到這一點:

- (void)addEvent { 

[self.tableScreen beginUpdates]; 

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
[self.tableScreen insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
         withRowAnimation:UITableViewRowAnimationFade]; 


[self.tableScreen reloadData]; 
[self.tableScreen endUpdates]; 

@try{ 
    [self.tableScreen scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
} 
@catch(NSException *exception){ 
} 

}

任何想法是什麼問題,這個時候???謝謝!

+0

你不應該把'reloadData'放在'beginUpdates'塊中。這兩件事是互相排斥的 – iwasrobbed 2014-05-05 18:49:17

22

當我忘記正確設置數據源插座時,我遇到了這個錯誤。

如果您看到此錯誤,檢查是否有明確設置你的TableView委託和數據源:

  • 去你的界面生成器,並期待在視圖與「表視圖」

  • cmd +鼠標右鍵拖拽(你應該看到一條藍線),從'Table View'圖標到該文件的標題圖標,在xcode 6旁邊有一個黃色圖標。

  • 釋放鼠標,你應該看到代表和da tasource選項。

  • 選擇 '數據源'

你的表,現在應該正確連接。

+3

對我來說解決了 – 2015-06-03 16:11:04

+0

這種方式對我來說是正確的。只需要在數據源和Table !!!!之間進行連線。和代表和表之間!不是視圖控制器。但是與表。 – 2018-01-11 10:17:08

0

選中此項「試圖將0行插入到0節,但更新後0節只有0行」。

當您在第0行0節中插入對象時沒有任何節,它會發出新信號。 嘗試在插入行之前插入節。

[timeZoneNames insertObject:@"HELLO" atIndex:0]; 
NSLog(@"sections: %lu ",[self.downlaodTableView numberOfSections]; 
// insert section 
[self.tableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)] withRowAnimation:UITableViewRowAnimationNone]; 
NSLog(@"numberOfRowsInSection: %d", [self tableView:self.tableView numberOfRowsInSection:0]); 
NSArray *insertIndexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; 
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop]; 
相關問題