How to remove UITableViewCell swipe to delete boun

2019-03-20 19:23发布

问题:

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:

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;
    }
}

回答1:

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);
    }
}


回答2:

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?



回答3:

try this for your custom delete button

https://gist.github.com/JigsChanchiya/7002903#file-customswipetodelete-mm