Last active
December 20, 2015 17:58
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MIT license, if anyone's interested.