重新排列表视图行会导致删除行时崩溃

时间:2013-05-24 11:19:16

标签: ios objective-c cocoa-touch uitableview

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

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

 }

2 个答案:

答案 0 :(得分:0)

此行会造成麻烦:

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

错误表明deleteObject:类中没有定义Circuit方法:

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

检查self.managedObjectContext后面有一个有效的对象。

答案 1 :(得分:0)

这一行之后:

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

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

unrecognized selector sent to instance