Question

I have a UITableView that displays the data from my plist. Is there any way I could edit/delete the row's data using UIMenuController?

It would be fine to use the traditional swipe-the-row in order to edit/delete the row, but I am using UISwipeGestureRecognizer to display my secondViewController - this view controller is where I could add a data to my plist.

The UIMenuItem shows up, but I don't know how to proceed from here.

It would be greatly appreciated if someone could walk me through.

This is my longPressHandler which triggers my UIMenuController.

- (void)longPressHandler:(UILongPressGestureRecognizer *)sender
{
    if ([sender state] == UIGestureRecognizerStateBegan) {
        [self becomeFirstResponder];

        CGPoint point = [sender locationInView:[self myTableView]];
        NSIndexPath *indexPath =[[self myTableView] indexPathForRowAtPoint:point];

        UIMenuController *menuController = [UIMenuController sharedMenuController];

        UIMenuItem *editFruit = [[UIMenuItem alloc] initWithTitle:@"Edit" action:@selector(editFruit)];
        UIMenuItem *deleteFruit = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(deleteFruit)];

        [menuController setTargetRect:[[self myTableView] rectForRowAtIndexPath:indexPath]
                               inView:[self myTableView]];
        [menuController setMenuItems:[NSArray arrayWithObjects:editFruit, deleteFruit, nil]];

        [menuController setMenuVisible:YES
                              animated:YES];
    }
}

This is where it ends.

- (void)deleteFruit
{
    NSLog(@"Delete.");
}

- (void)editFruit
{
    NSLog(@"Edit.");
}
Was it helpful?

Solution

It looks like you have all you need there.

So in your deleteFruit method, simply delete the item at that indexPath you've found, and call -deleteRowsAtIndexPaths:withRowAnimation: on the tableview.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top