【问题标题】:Re Arranging table view rows causes crash on delete of row重新排列表视图行导致删除行时崩溃
【发布时间】:2013-05-24 11:19:15
【问题描述】:

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

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

 }

【问题讨论】:

    标签: ios objective-c cocoa-touch uitableview


    【解决方案1】:

    这行会引起麻烦:

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

    错误说deleteObject:方法没有在Circuit类中定义:

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

    检查self.managedObjectContext在这行之后有一个有效的对象。

    【讨论】:

      【解决方案2】:

      在这一行之后:

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

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

      unrecognized selector sent to instance
      

      【讨论】:

      • 感谢您的回复,好的,我明白您的意思了,您能否建议如何解决该错误,更完整的答案也将帮助其他可能遇到此问题的 Stack Overflow 用户。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-13
      • 1970-01-01
      相关资源
      最近更新 更多