Created by Jamz Tang at 13 September 2012
- (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.
Clone this repository:
git clone git://gist.github.com/3009826.git UITableViewDeleteActionResponder
If you think this is useful, share this article with your friends :)
blog comments powered by DisqusUINibDecoderProxy Observes what's encoded in an NSCoder object
JTKeyValueObserver Revisiting KVO+Block, the simplest version.
MethodSwizzle Method Swizzling in Objective-C
UITableViewDeleteActionResponder Quick hack to enable delete menu item in UITableView menuController
UIApplicationAddition Quickly switch supported UIInterfaceOrientation for your View Controllers
JTTargetActionBlock Adding Block support for UIControl's Target-Action mechanism
NSArray-JTArraySplit Splitting an array to several components
UIImage+JTImageDecode Force decompressing UIImage in background to achieve better performance
UINavigationBar-JTDropShadow Adding drop shadow on UINavigationBar (before iOS 6)
UIImage-JTImageCrop Crop an image in specific rect
UIView+JTRemoveAnimated Adding fadeout effect to any -[UIViews removeFromSuperview]
JTStringAddition NSStringf. Simpler printf styled +[NSString stringWithFormat:]
UIView-JTViewToImage Rendering any UIViews into UIImage in one line (updated with iOS 7 support)
NSObject-JTNibLoader Loading a Nib file programmatically using NSObject category
UIImage-JTColor Creating a placeholder UIImage dynamically with color
NSObject-JTCancelableScheduledBlock Cancelable Scheduled Blocks in Objective-C