更新中に tableView に実際に含まれていた行とセクションの数を出力してみました。これにより、テーブル ビュー データ ソース メソッドで返されるセクションの数がわかりました。これが原因でした。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 0;
}
0 ではなく 1 を返すようにしてください。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
少なくともこれで問題は解決しました...
アップデート:
しばらく動作していた後、同じ問題が再び発生しています。アプリを少し修正しました。私の最初のビューは tableView です。右上のプラス ボタンをクリックすると、情報を入力できる別のテーブルが表示されます。[完了] をクリックすると、情報が次のコードで Core Data モデルに追加されます。
- (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 メソッドで次のようにコンソールに出力することで、Core Data モデルに正常に配置されることを確認しました。
-(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){
}
}
今回の問題は何か分かりますか?ありがとう!