Я пытаюсь переопределить tableView:commitEditingStyle:editingStyleforRowAtIndexPath:
и не могу реализовать удаление фактического экземпляра NSManagedObject
, представленного в этой строке.
Apple говорит, что это должно быть сделано с помощью следующего кода ( Здесь показано ):
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object at the given index path.
NSManagedObject *eventToDelete = [eventsArray objectAtIndex:indexPath.row];
[managedObjectContext deleteObject:eventToDelete];
// Update the array and table view.
[eventsArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
// Commit the change.
NSError *error = nil;
if (![managedObjectContext save:&error]) {
// Handle the error.
}
}
}
Когда я имитирую этот пример кода в моем приложении, каждая строка работает, за исключением одной строки. Одна строка: [bowlerArray removeObjectAtIndex:indexPath.row];
. Я получаю сообщение об ошибке «Тип получателя NSArray, например, сообщение не объявляет метод с селектором« removeObjectAtIndex »».
Какой должна быть эта строка кода?
Примечание: моя строка NSManagedObject *eventToDelete = [bowlerArray objectAtIndex:indexPath.row];
отлично работает.
Обновление: отправка моего фактического кода:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *moc = [appDelegate managedObjectContext];
NSManagedObject *objectToDelete = [bowlerArray objectAtIndex:indexPath.row];
[moc deleteObject:objectToDelete];
[bowlerArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}