如何删除UITableViewCell滑动以删除弹跳 [英] How to remove UITableViewCell swipe to delete bounce

查看:121
本文介绍了如何删除UITableViewCell滑动以删除弹跳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 7中新的滑动删除外观增加了反弹效果,UITableViewCell在滑动后继续偏移。是否有任何方法可以禁用此反弹,以便一旦删除按钮完全可见,单元格就会停止?

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?

继续偏移的单元格:
< br>

Cell that continues to offset:

我想要的即使继续拖动,单元格也会在此处停止:


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

我在我的 cellForRowAtIndexPath:方法中试过这个,但似乎没有任何改变。

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


推荐答案

我我想我终于找到了解决方案!使用自定义单元格,您可以将该单元格设置为 UIScrollViewDelegate 并实现 scrollViewDidScroll:方法。在该方法中,您可以强制UIScrollView的contentOffset保持在特定值(我使用 82.0f ,因为当'删除'按钮时,这似乎是contentOffset完全可见)。像这样:

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

这也可以在不使用自定义单元格的情况下完成只需将ViewController设置为 UIScrollViewDelegate ,并在 tableView:cellForRowAtIndexPath 中设置UIScrollView的委托,如下所示:

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

这篇关于如何删除UITableViewCell滑动以删除弹跳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆