2013-09-26 105 views
5

在iOS 7中新的“轻扫删除”外观添加了“反弹”效果,其中UITableViewCell在轻扫后继续偏移。有什么办法可以禁用这种反弹,这样一旦删除按钮完全可见,单元格就会硬停止。是继续如何删除UITableViewCell轻扫以删除反弹

细胞,以抵消:

我想要的细胞停在这里,即使拖着继续:

enter image description here

我在cellForRowAtIndexPath:方法试过,但似乎没有什么更改。

for(UIView *subview in cell.subviews){ 
    if([subview isKindOfClass:[UIScrollView class]]){ 
     UIScrollView *theScrollView = (UIScrollView *)subview; 
     theScrollView.bounces = NO; 
    } 
} 
+0

你得到这个工作?如果你看看集成的天气应用程序,它会做你想做的事情,我一直在寻找如何做到这一点,现在仍然没有任何东西。 –

+0

@AlexSaidani - 没有。我一直在尝试,但仍然没有解决方案。你说得对!我知道我曾经在某个地方看过它,但却不记得在哪里。 – hgwhittle

回答

4

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

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

这也可以不使用自定义单元格通过简单地设置一个视图控制器作为UIScrollViewDelegate和设置完成UIScrollView的代表tableView:cellForRowAtIndexPath像这样:

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

我不认为有针对的选择,但也许你可以做的是子类的细胞和didTransitionToState:您可以检测删除确认状态。

现在在这一点上我不完全确定你可以做什么来防止滚动,但我希望这会让你在正确的方向。

也许你可以在这个状态禁用细胞的手势识别器?