Created by Jamz Tang at 13 September 2012

Quick hack to enable delete menu item in UITableView menuController

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

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    NSLog(@"%@", NSStringFromSelector(action));
    return YES;
}

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
}

Watch carefully, there is a problem. So you found yourself returning YES for all of the actions including the delete: selector, and the delete item just doesn't show up.

To properly fix it, you should really subclass your UITableViewCell and implement your own delete: method.

But if you would like a really quick hack for all of your default tableViewCell, you can do it with category and implements the delete: method.


After the import of snippets:

// By only returning YES for delete menu item, we can get the results like the screenshot down below.
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action
forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return action == @selector(delete:);
}

The category also helps you to route the delete: selector to your tableViewDelegate's tableView:performAction:forRowAtIndexPath:withSender: method, so you can handle it like all other actions.

Updated: 30 June 2012

The version you see above, I considered as a more elegant version for it's clever use of responder chain. Worth a look to the old naive version if you're digging down to the ground.

This is revised version from the old blog post.


Installation

Clone this repository:
git clone git://gist.github.com/3009826.git UITableViewDeleteActionResponder

Download | More...


If you think this is useful, share this article with your friends :)

blog comments powered by Disqus

ioscodesnippet.com