2012-07-29 5 views
35

UITableViewControllerを使用していて、tableViewの更新中にこのエラーが発生しました。以下は私のコードです:私はclickイベントを行う際NSInternalInconsistencyException '、reason:'セクション0に行0を挿入しようとしましたが、更新後にセクション0に0行しかありません。 '

これが発生します。

[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のが助けました。

おかげで、私は前にこの問題に遭遇しました ABY

答えて

27

。私は-tableView:numberOfRowsInSection:の戻り値があるのだろう更新


を:それは(-beginUpdates参照)-insertRowsAtIndexPaths:withRowAnimation:が呼び出されたとき-tableView:numberOfRowsInSection:あなたはあなたが-insertRowsAtIndexPaths:withRowAnimation:を呼び出す前に、モデルに挿入する必要があり0が戻されたことを意味し?また、アップデートが1つしかない場合は、-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

+0

numberOfRowsInSectionの戻り値の型は[timeZoneNames count]です。 beginUpdatesとendUpdatesを使用せずにinsertRowsAtIndexPathsを使用しても問題ありませんか? – insomiac

+0

@Aby '-beginUpdates'のドキュメント:以降の挿入、削除、および選択操作(たとえば、cellForRowAtIndexPath:とindexPathsForVisibleRows)を同時にアニメーション化する場合は、このメソッドを呼び出します。複数のUI操作を1つのUI操作にグループ化するために使用されます。操作は1つのみです。 –

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'ブロックの中に置かないでください。それらの2つのものは相互に排他的です – iwasrobbed

22

データソースを適切に設定するのを忘れたときにこのエラーが発生しました。

  • は、あなたのインターフェイスビルダーに移動し、

  • 「テーブルビュー」でビューを見て:

    このエラーが表示された場合、あなたが持っていることを確認し、明示的にあなたのテーブルビューのデリゲートとデータソースを設定しますcmd +「テーブルビュー」アイコンからファイルのタイトルアイコンまで右クリックしてドラッグすると(青色の線が表示されます)、xcode 6の横に黄色いアイコンが表示されます。

  • マウスを離します。デリゲートとdaを参照する必要がありますtasourceオプション。

  • あなたのテーブルが正しく配線する必要があり

を 'データソース' を選択します。

+3

私のためにそれを解決しました –

+0

この方法は私にとっては正しかったです。 datasourceとTable !!!!の間の配線を行うだけです。デリゲートとテーブルの間に!!!コントローラを表示しません。しかし、テーブル。 –

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]; 
関連する問題