1

在我的表格视图中删除行可以正常工作。但是,如果我重新安排一行,我会崩溃:

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

 }
4

2 回答 2

0

这条线会引起麻烦:

[self.managedObjectContext deleteObject:[self.circuits objectAtIndex:indexPath.row]];

该错误表明该deleteObject:方法未在Circuit类中定义:

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

Checkself.managedObjectContext在此行之后有一个有效对象。

于 2013-05-24T11:40:46.007 回答
0

在这一行之后:

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

managedObjectContext 变量更改为 Circuit 对象。因此,您操作 NSManagedObjectContext 的任何方法都会导致错误:

unrecognized selector sent to instance
于 2013-05-24T13:35:31.640 回答