2013-10-28 27 views
0

*終止應用程序由於未捕獲的異常「NSInternalInconsistencyException」,理由是:「試圖插入一行0到段0,但只有0行第0更新後」insertRowsatIndexPath例外的iOS

我知道這裏的問題是我試圖添加一個單元格到tableView,但它不希望它不添加一個,我不明白爲什麼。

- (IBAction)addNewIngredient: (id) sender 
{ 
    NSString *newIngredient = [recipe createIngredient]; 
    [[recipe ingredients]addObject:newIngredient]; 

    //figure out where that item is in the array 
    int lastRow = [[recipe ingredients]indexOfObject: newIngredient]; 

    NSIndexPath *ip = [NSIndexPath indexPathForRow:lastRow inSection:0]; 

    NSLog(@"Added a new ingredient!"); 

    [[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation: 
    UITableViewRowAnimationTop]; 


} 

爲了澄清什麼,我想在這裏做的事:

配方有一個屬性成分是NSString的數組。 我正在添加一個對象到成分數組(這是否會自動增加數組數或我必須手動執行它?)...

然後我搞清楚那個項目在數組中的位置,用「 int lastRow = [[recipe ingredients] indexOfObject:newIngredient]「 然後獲取索引路徑指向那裏。然後將該成分插入到tableView中。

+0

什麼是'recipe'?它存在嗎? – Wain

回答

1

您的例外的原因是,當您插入該行時,您的numberOfRowsInSection將再次被調用。

在那個時候,您的數據需要進行更新,因此,如果你有0行,並插入一個進去,函數返回1

+0

好吧,這是有道理的。我的問題是,當我添加Object:newIngredient到我的數組,不會增加1的數據計數? – user1657563

+0

嗯,是的,但是你在'numberOfRowsInSection'中檢查第0部分的數組嗎?仔細調試它,你會發現錯誤 – Ismael