Skip to content

Instantly share code, notes, and snippets.

@mflint
Last active December 20, 2015 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mflint/6171916 to your computer and use it in GitHub Desktop.
Save mflint/6171916 to your computer and use it in GitHub Desktop.
A subclass of UITableViewCell which slides to the left when the 'swipe to delete' gesture happens
#import "SlidesToLeftOnDeleteTableViewCell.h"
@interface SlidesToLeftOnDeleteTableViewCell ()
@property(nonatomic, assign) CGFloat deleteConfirmationShift;
@end
@implementation SlidesToLeftOnDeleteTableViewCell
- (void)prepareForReuse {
[super prepareForReuse];
self.deleteConfirmationShift = 0;
}
- (void)layoutSubviews {
CGFloat textLabelWidth = self.textLabel.frame.size.width;
[super layoutSubviews];
if (!self.showingDeleteConfirmation && self.deleteConfirmationShift > 0) {
// move the [Delete] button back to where it should be,
// so it doesn't whizz off the right hand side of the screen
for (UIView *subview in self.subviews) {
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
CGRect newFrame = subview.frame;
newFrame.origin.x -= self.deleteConfirmationShift;
subview.frame = newFrame;
}
}
// move self.frame back, and reduce its width again
CGRect newFrame = self.frame;
newFrame.origin.x += self.deleteConfirmationShift;
newFrame.size.width -= self.deleteConfirmationShift;
// NOTE: important to set this to zero....
self.deleteConfirmationShift = 0;
// ... before setting "self.frame", because "self.frame = ..."
// causes a synchronous call to [self layoutSubviews] !!
self.frame = newFrame;
}
// if shift is positive, then the delete button is appearing
CGFloat shift = textLabelWidth - self.textLabel.frame.size.width;
if (self.showingDeleteConfirmation && shift != 0) {
self.deleteConfirmationShift = shift;
for (UIView *subview in self.contentView.subviews) {
CGRect newFrame = subview.frame;
// compensate for the [super layoutSubViews] changing the width
newFrame.size.width += self.deleteConfirmationShift;
subview.frame = newFrame;
}
{
CGRect newFrame = self.frame;
newFrame.origin.x -= self.deleteConfirmationShift;
newFrame.size.width += self.deleteConfirmationShift;
self.frame = newFrame;
}
}
}
@end
@mflint
Copy link
Author

mflint commented Aug 7, 2013

MIT license, if anyone's interested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment