Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove UITableViewCell swipe to delete bounce

The new 'swipe to delete' look and feel in iOS 7 added a 'bounce' effect where the UITableViewCell continues to offset after a swipe. Is there any way to disable this bounce, so that the cell makes a hard stop once the delete button is fully visible?

Cell that continues to offset:

I want the cell to stop here even if dragging continues:

enter image description here

I tried this in my cellForRowAtIndexPath: method, but nothing seemed to change.

for(UIView *subview in cell.subviews){
    if([subview isKindOfClass:[UIScrollView class]]){
        UIScrollView *theScrollView = (UIScrollView *)subview;
        theScrollView.bounces = NO;
    }
}
like image 435
hgwhittle Avatar asked Sep 26 '13 18:09

hgwhittle


2 Answers

I think I finally found a solution! Using a custom cell, you can set that cell as a UIScrollViewDelegate and implement the scrollViewDidScroll: method. In that method, you can force the UIScrollView's contentOffset to stay under a particular value (I'm using 82.0f because that seems to be the contentOffset when the 'Delete' button is fully visible). Like this:

.h

@interface MyCustomCell : UITableViewCell <UIScrollViewDelegate>

.m

-(void)awakeFromNib{
    [super awakeFromNib];

    for(UIView *subview in self.subviews){
        if([subview isKindOfClass:[UIScrollView class]]){
            UIScrollView *theScrollView = (UIScrollView *)subview;
            theScrollView.delegate = self;
        }
    }
}

#pragma mark - UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    static CGFloat kTargetOffset = 82.0f; 

    if(scrollView.contentOffset.x >= kTargetOffset){
        scrollView.contentOffset = CGPointMake(kTargetOffset, 0.0f);
    }
}

This can also be done without using a custom cell by simply setting a ViewController as a UIScrollViewDelegate and setting the UIScrollView's delegate in tableView:cellForRowAtIndexPath like so:

.h

MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    for(UIView *subview in cell.subviews){
        if([subview isKindOfClass:[UIScrollView class]]){
            UIScrollView *theScrollView = (UIScrollView *)subview;
            theScrollView.delegate = self;
        }
    }

    return cell;
}

#pragma mark - UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    static CGFloat kTargetOffset = 82.0f;

    if(scrollView.contentOffset.x >= kTargetOffset){
        scrollView.contentOffset = CGPointMake(kTargetOffset, 0.0f);
    }
}
like image 132
hgwhittle Avatar answered Sep 21 '22 22:09

hgwhittle


I dont think there is an option for that but perhaps what you can do is subclass your cell and in didTransitionToState: you can detect the delete confirmation state.

Now at this point im not entirely sure what you can do to prevent the scrolling but I hope this puts you in the right direction.

Maybe you can disable the cell's gesture recognizer in this state?

like image 31
KDaker Avatar answered Sep 19 '22 22:09

KDaker