Skip to content

Commit b6d9a80

Browse files
committed
Merge pull request #272 from pbtura/master
Fix for issue #264
2 parents 201989e + 0538bcd commit b6d9a80

File tree

2 files changed

+57
-49
lines changed

2 files changed

+57
-49
lines changed

SWTableViewCell/PodFiles/SWTableViewCell.m

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ - (BOOL)shouldHighlight;
4545

4646
@implementation SWTableViewCell {
4747
UIView *_contentCellView;
48+
BOOL layoutUpdating;
4849
}
4950

5051
#pragma mark Initializers
@@ -75,6 +76,7 @@ - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSStr
7576

7677
- (void)initializer
7778
{
79+
layoutUpdating = NO;
7880
// Set up scroll view that will host our cell content
7981
self.cellScrollView = [[SWCellScrollView alloc] init];
8082
self.cellScrollView.translatesAutoresizingMaskIntoConstraints = NO;
@@ -317,6 +319,7 @@ - (void)layoutSubviews
317319

318320
- (void)setFrame:(CGRect)frame
319321
{
322+
layoutUpdating = YES;
320323
// Fix for new screen sizes
321324
// Initially, the cell is still 320 points wide
322325
// We need to layout our subviews again when this changes so our constraints clip to the right width
@@ -325,7 +328,10 @@ - (void)setFrame:(CGRect)frame
325328
[super setFrame:frame];
326329

327330
if (widthChanged)
331+
{
328332
[self layoutIfNeeded];
333+
}
334+
layoutUpdating = NO;
329335
}
330336

331337
- (void)prepareForReuse
@@ -575,59 +581,62 @@ - (CGPoint)contentOffsetForCellState:(SWCellState)state
575581

576582
- (void)updateCellState
577583
{
578-
// Update the cell state according to the current scroll view contentOffset.
579-
for (NSNumber *numState in @[
580-
@(kCellStateCenter),
581-
@(kCellStateLeft),
582-
@(kCellStateRight),
583-
])
584+
if(layoutUpdating == NO)
584585
{
585-
SWCellState cellState = numState.integerValue;
586+
// Update the cell state according to the current scroll view contentOffset.
587+
for (NSNumber *numState in @[
588+
@(kCellStateCenter),
589+
@(kCellStateLeft),
590+
@(kCellStateRight),
591+
])
592+
{
593+
SWCellState cellState = numState.integerValue;
594+
595+
if (CGPointEqualToPoint(self.cellScrollView.contentOffset, [self contentOffsetForCellState:cellState]))
596+
{
597+
_cellState = cellState;
598+
break;
599+
}
600+
}
601+
602+
// Update the clipping on the utility button views according to the current position.
603+
CGRect frame = [self.contentView.superview convertRect:self.contentView.frame toView:self];
604+
frame.size.width = CGRectGetWidth(self.frame);
605+
606+
self.leftUtilityClipConstraint.constant = MAX(0, CGRectGetMinX(frame) - CGRectGetMinX(self.frame));
607+
self.rightUtilityClipConstraint.constant = MIN(0, CGRectGetMaxX(frame) - CGRectGetMaxX(self.frame));
586608

587-
if (CGPointEqualToPoint(self.cellScrollView.contentOffset, [self contentOffsetForCellState:cellState]))
609+
if (self.isEditing) {
610+
self.leftUtilityClipConstraint.constant = 0;
611+
self.cellScrollView.contentOffset = CGPointMake([self leftUtilityButtonsWidth], 0);
612+
_cellState = kCellStateCenter;
613+
}
614+
615+
self.leftUtilityClipView.hidden = (self.leftUtilityClipConstraint.constant == 0);
616+
self.rightUtilityClipView.hidden = (self.rightUtilityClipConstraint.constant == 0);
617+
618+
if (self.accessoryType != UITableViewCellAccessoryNone && !self.editing) {
619+
UIView *accessory = [self.cellScrollView.superview.subviews lastObject];
620+
621+
CGRect accessoryFrame = accessory.frame;
622+
accessoryFrame.origin.x = CGRectGetWidth(frame) - CGRectGetWidth(accessoryFrame) - kAccessoryTrailingSpace + CGRectGetMinX(frame);
623+
accessory.frame = accessoryFrame;
624+
}
625+
626+
// Enable or disable the gesture recognizers according to the current mode.
627+
if (!self.cellScrollView.isDragging && !self.cellScrollView.isDecelerating)
588628
{
589-
_cellState = cellState;
590-
break;
629+
self.tapGestureRecognizer.enabled = YES;
630+
self.longPressGestureRecognizer.enabled = (_cellState == kCellStateCenter);
631+
}
632+
else
633+
{
634+
self.tapGestureRecognizer.enabled = NO;
635+
self.longPressGestureRecognizer.enabled = NO;
591636
}
592-
}
593-
594-
// Update the clipping on the utility button views according to the current position.
595-
CGRect frame = [self.contentView.superview convertRect:self.contentView.frame toView:self];
596-
frame.size.width = CGRectGetWidth(self.frame);
597-
598-
self.leftUtilityClipConstraint.constant = MAX(0, CGRectGetMinX(frame) - CGRectGetMinX(self.frame));
599-
self.rightUtilityClipConstraint.constant = MIN(0, CGRectGetMaxX(frame) - CGRectGetMaxX(self.frame));
600-
601-
if (self.isEditing) {
602-
self.leftUtilityClipConstraint.constant = 0;
603-
self.cellScrollView.contentOffset = CGPointMake([self leftUtilityButtonsWidth], 0);
604-
_cellState = kCellStateCenter;
605-
}
606-
607-
self.leftUtilityClipView.hidden = (self.leftUtilityClipConstraint.constant == 0);
608-
self.rightUtilityClipView.hidden = (self.rightUtilityClipConstraint.constant == 0);
609-
610-
if (self.accessoryType != UITableViewCellAccessoryNone && !self.editing) {
611-
UIView *accessory = [self.cellScrollView.superview.subviews lastObject];
612637

613-
CGRect accessoryFrame = accessory.frame;
614-
accessoryFrame.origin.x = CGRectGetWidth(frame) - CGRectGetWidth(accessoryFrame) - kAccessoryTrailingSpace + CGRectGetMinX(frame);
615-
accessory.frame = accessoryFrame;
616-
}
617-
618-
// Enable or disable the gesture recognizers according to the current mode.
619-
if (!self.cellScrollView.isDragging && !self.cellScrollView.isDecelerating)
620-
{
621-
self.tapGestureRecognizer.enabled = YES;
622-
self.longPressGestureRecognizer.enabled = (_cellState == kCellStateCenter);
638+
self.cellScrollView.scrollEnabled = !self.isEditing;
623639
}
624-
else
625-
{
626-
self.tapGestureRecognizer.enabled = NO;
627-
self.longPressGestureRecognizer.enabled = NO;
628-
}
629-
630-
self.cellScrollView.scrollEnabled = !self.isEditing;
631640
}
632641

633642
#pragma mark - UIScrollViewDelegate

SWTableViewCell/ViewController.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ - (void)viewDidLoad {
3535
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
3636
[refreshControl addTarget:self action:@selector(toggleCells:) forControlEvents:UIControlEventValueChanged];
3737
refreshControl.tintColor = [UIColor blueColor];
38-
39-
[self.tableView addSubview:refreshControl];
38+
4039
self.refreshControl = refreshControl;
4140

4241
// If you set the seperator inset on iOS 6 you get a NSInvalidArgumentException...weird

0 commit comments

Comments
 (0)