Crash during UITableView of moveRowAtIndexPath Method

I have an array of strings that I already show in my uitableview.

When editing is enabled for my uitableview, I try to wrap the line from the 4th line to the 3rd line, and I get EXC_BAD_ACCESS in the insert object in the index part of my code when I debug this.

[myStringsArray insertObject:name atIndex:toIndexPath.row]; 

However, if I do not debug and just do not build and run, I can print the next line and then it crashes without seeing EXC_BAD_ACCESS in my console.

NSLog(@"re-ordered myStringsArray array");

Here is my code for the moveRowAtIndexPath method:

// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:@"MyStrings.plist"]; 

    //re-order array
    NSString *name = [myStringsArray objectAtIndex:fromIndexPath.row];
    [myStringsArray removeObjectAtIndex:fromIndexPath.row];
    [myStringsArray insertObject:name atIndex:toIndexPath.row];

    NSLog(@"re-ordered myStringsArray array");
    if([myStringsArray writeToFile:path atomically: YES]){NSLog(@"write succesful");}
    else {NSLog(@"write failed");}

    [myTable reloadData];
}

Any ideas why he thinks I'm trying to access something that doesn't exist?

Correct me if I am wrong, but my modified array is already allocated and if the delete method worked, why not use the insert method?

thank

+3
4

, , , 0, . , :

NSString *name = [[myStringsArray objectAtIndex:fromIndexPath.row] retain];
[myStringsArray removeObjectAtIndex:fromIndexPath.row];
[myStringsArray insertObject:name atIndex:toIndexPath.row];
[name release];

NSMutableArray , :

[myStringsArray exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
+5

, , , , , , , ( ). , , , UITableView , , , , , , , , EXE kk.

, , , .

[myArray exchangeObjectAtIndex: fromIndexPath.row    withObjectAtIndex: toIndexPath.row];

, .

  • (void) tableView: (UITableView *) tableView moveRowAtIndexPath: (NSIndexPath *) fromIndexPath toIndexPath: (NSIndexPath *) toIndexPath
0

In the TableView delegate method, do the following with myStringArray (as a DataView of a TableView data source)

 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath 
 {   
   NSString *name = [[myStringsArray objectAtIndex:sourceIndexPath.row] retain];
   [myStringsArray removeObjectAtIndex:sourceIndexPath.row];
   [myStringsArray insertObject:name atIndex:destinationIndexPath.row];
   [name release];
 }

And add two new delegates to your .h file, like this

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{    
  return YES;
}

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
   toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath 
{
  return proposedDestinationIndexPath;
}
0
source

in the declaration of myStringsArray, write as nmutable arrary initwithObjects, nil] and make sure that the property in the .h file is strong.

0
source

Source: https://habr.com/ru/post/1793546/


All Articles