iOS 7 中新的“滑动删除”外观添加了“弹跳”效果,其中 UITableViewCell 在滑动后继续偏移。有没有什么方法可以禁用这种反弹,以便一旦删除按钮完全可见,单元格就会硬停止?
Cell that continues to offset:
![](https://i.stack.imgur.com/IxdyJ.png)
I want the cell to stop here even if dragging continues:
![enter image description here](https://i.stack.imgur.com/gIsjq.png)
我在我的cellForRowAtIndexPath:
方法,但似乎没有任何改变。
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)。像这样:
.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
并设置 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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)