2013-05-24 5 views
1

Удаление строк в моем представлении таблицы прекрасно работает. Однако если я вновь организовать ряд я получаю аварию:Re Упорядочение строк таблицы вызывает сбой при удалении строки

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Circuit deleteObject:]: unrecognized selector sent to instance 0xa4dafc0'

Я понимаю, что он компилятор говорит мне, но не знает, как адресовать его.

- (id)initWithStyle:(UITableViewStyle)style andDistributionBoard:(DistributionBoard *)distributionBoard 
{ 
    self = [super initWithStyle:style]; 
if (self) { 
    self.distributionBoard = distributionBoard; 
    [self loadData]; 

    //delete row 
    if (self) { 

     self.appDelegate = [[UIApplication sharedApplication] delegate]; 
     self.managedObjectContext = self.appDelegate.managedObjectContext; 

     [self loadData]; 

     } 
    } 
return self; 
} 

- (void)loadData 
{ 
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"createdAt" ascending:YES]; 
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
self.circuits = [[self.distributionBoard.circuits sortedArrayUsingDescriptors:sortDescriptors] mutableCopy]; 
} 


- (void)addPressed:(id)sender <------- adds rows to the table view 
{ 
LogCmd(); 
Circuit *circuit = [[ICCircuitManager manager] newCircuit]; 
circuit.distributionBoard = self.distributionBoard; 
circuit.circuitReference = [NSString stringWithFormat:@"%d", [self.circuits count] + 1]; 
circuit.createdAt = [NSDate date]; 
circuit.modifiedAt = [NSDate date]; 
[self.distributionBoard addCircuitsObject:circuit]; 
[self loadData]; 
[self.tableView reloadData]; 


[[[ICAbstractManager alloc] init].appDelegate saveContext]; 

} 



///other code 



//////////table view editing 

// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// Return NO if you do not want the item to be re-orderable. 
return YES; 
} 
//stop first circuit being deleted 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return indexPath.row > 0; 
} 
//Delete circuits 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

if (editingStyle == UITableViewCellEditingStyleDelete) { 
[self.managedObjectContext deleteObject:[self.circuits objectAtIndex:indexPath.row]]; 
[self.appDelegate saveContext]; 
[self.circuits removeObjectAtIndex:indexPath.row]; 

[tableView endUpdates]; 
[tableView reloadData]; 


    } 

} 
//Allow table row re arrranging 
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { 

NSObject *tempObj = [self.circuits objectAtIndex:sourceIndexPath.row]; 


self.managedObjectContext = [self.circuits objectAtIndex:sourceIndexPath.row]; 
[self.circuits removeObjectAtIndex:sourceIndexPath.row]; 
[self.circuits insertObject:tempObj atIndex:destinationIndexPath.row]; 
[self.appDelegate saveContext]; 

} 

ответ

0

Эта линия вызывает тревогу:

ошибка говорит о том, что метод deleteObject: не определен в Circuit классе:

self.managedObjectContext = [self.circuits objectAtIndex:sourceIndexPath.row]; 
[self.circuits removeObjectAtIndex:sourceIndexPath.row]; 

Проверить self.managedObjectContext имеет действительный объект после этой строки.

0

После этой линии:

self.managedObjectContext = [self.circuits objectAtIndex:sourceIndexPath.row]; 

Переменная managedObjectContext изменяется на объект Circuit. Таким образом, вы управляете любым методом NSManagedObjectContext вызовут ошибку:

unrecognized selector sent to instance 
+0

Спасибо за ответ, Хорошо, я понимаю, что ваше высказывание, не могли бы вы посоветовать, как исправить ошибку, более полный ответ также поможет другим пользователям Stack Переполнение, которое может столкнуться с этим вопросом. – JSA986

Смежные вопросы