40

UITableViewController を使用していますが、tableView の更新中にこのエラーが発生します。以下は私のコードです:

これは、クリックイベントを実行すると発生します:

[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];

アップルのドキュメントを探してみましたが、役に立ちませんでした。

ありがとう、アビー

4

4 に答える 4

28

私は以前にこの問題に遭遇しました。これは、-insertRowsAtIndexPaths:withRowAnimation:呼び出されたときに-tableView:numberOfRowsInSection:0 が返されたことを意味します。呼び出す前にモデルに挿入する必要があります-insertRowsAtIndexPaths:withRowAnimation:(参照: -beginUpdates)


アップデート

の戻り値-tableView:numberOfRowsInSection:は何ですか?また、アップデートが 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];
于 2012-07-29T04:13:38.477 に答える
4

更新中に 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){
}

}

今回の問題は何か分かりますか?ありがとう!

于 2013-02-16T05:09:31.490 に答える
-1

この「行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];
于 2016-10-14T02:30:12.920 に答える