テーブル ビューでの行の削除は正常に機能します。ただし、行を再配置するとクラッシュします。
*** 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];
}