i have custom UITableViewCells composed of their own delete buttons. i'm currently attempting to install undo with unlimited (or at least 20ish) levels with hackish results.
this is so far the best i've come up with, but it's designed to only support one level of undo AND the code is super ghetto:
additionally, this code inserts the undone delete at the bottom of the table, whereas it would be more ideal to place it back to it's original index. tried some stuff with insertRowsAtIndexPaths but it made me crazy (crashed all the time!). what is the best way to achieve this functionality?
this is so far the best i've come up with, but it's designed to only support one level of undo AND the code is super ghetto:
Code:
- (IBAction)deleteMeasurement:(id)sender
{
[COLOR="SeaGreen"]//Obtain Index Path Of Deleting Row[/COLOR]
NSIndexPath *indexPath = [self.measurementsTableView indexPathForCell:(UITableViewCell *)[[[sender superview] superview] superview]];
NSUInteger row = [indexPath row];
[COLOR="SeaGreen"]//Backup Deleting Dictionary Entry As String Variables[/COLOR]
NSMutableDictionary *item = [self.measurementsDataArray objectAtIndex:row];
self.deletedDictionaryName = [NSString stringWithFormat:@"%@", [item objectForKey:@"name"]];
self.deletedDictionaryMeasurement = [NSString stringWithFormat:@"%@", [item objectForKey:@"measurement"]];
self.deletedDictionaryUnit = [NSString stringWithFormat:@"%@", [item objectForKey:@"unit"]];
[COLOR="SeaGreen"]//Delete Dictionary Entry From Measurements Data Array[/COLOR]
[self.measurementsDataArray removeObjectAtIndex:row];
[self.measurementsTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[[undoManager prepareWithInvocationTarget:self] undoDeleteMeasurementAtIndexPath:indexPath];
[undoManager setActionName:NSLocalizedString(SMConstTableViewDeleteButtonLabel, nil)];
}
- (void)undoDeleteMeasurementAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *dictionaryItem = [[NSMutableDictionary alloc] init];
[dictionaryItem setObject:[NSString stringWithFormat:@"%@", self.deletedDictionaryName] forKey:@"name"];
[dictionaryItem setObject:[NSString stringWithFormat:@"%@", self.deletedDictionaryMeasurement] forKey:@"measurement"];
[dictionaryItem setObject:[NSString stringWithFormat:@"%@", self.deletedDictionaryUnit] forKey:@"unit"];
[measurementsDataArray addObject:dictionaryItem];
NSUInteger row = [indexPath row];
[measurementsDataArray insertObject:dictionaryItem atIndex:row];
[measurementsDataArray removeObjectAtIndex:row];
[dictionaryItem release];
}
additionally, this code inserts the undone delete at the bottom of the table, whereas it would be more ideal to place it back to it's original index. tried some stuff with insertRowsAtIndexPaths but it made me crazy (crashed all the time!). what is the best way to achieve this functionality?