在 tableView 中,willBeginEditingRowAtIndexPath
在非常轻微的单元格滑动时被触发以进入编辑模式。当 3 个 UITableViewRowAction
按钮暴露时,手势向左拉的时间不够长,无法让单元格留在左侧。相反,单元格会滑回右侧。但是,didEndEditingRowAtIndexPath
不会自动触发。只有随后的触摸手势才会触发该方法。为什么单元格向右滑回时didEndEditingRowAtIndexPath
不触发?当手势结束和/或单元格返回到与右侧对齐的正常状态时,我该如何拾取?
或者换句话说。 willBeginEditingRowAtIndexPath
被触发并将我的屏幕状态更改为编辑状态。但是,由于一直没有进入编辑阶段,我需要将屏幕状态改回非编辑状态。 didEndEditingRowAtIndexPath
会这样做,但直到再次触摸时才会调用它,这会让用户感到困惑。是否有另一个指示器,也许在 UIControl
中可以触发我可以使用?
更多信息:UITableViewRowAction 按钮是基于以下 repo 的子类:https://github.com/benguild/BGTableViewRowActionWithImage .虽然使用标准 UITableViewRowAction
进行测试并没有解决问题。
最佳答案
虽然这是一个 iOS 错误,但我找到了解决方法。
在您的单元格中,添加以下代码:
@interface UITableViewCell () <UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIPanGestureRecognizer *panRecognizer;
@property (nonatomic, assign) BOOL isShowingDeleteConfirmationMask;
@end
-(void)awakeFromNib
{
[super awakeFromNib];
self.panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecongizer:)];
self.panRecognizer.delegate = self;
[self.contentView addGestureRecognizer:self.panRecognizer];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (void)panGestureRecongizer:(UIPanGestureRecognizer *)recognizer
{
switch (recognizer.state) {
case UIGestureRecognizerStateEnded:
[self refreshCellState];
break;
case UIGestureRecognizerStateCancelled:
[self refreshCellState];
break;
default:
break;
}
}
- (void)didTransitionToState:(UITableViewCellStateMask)state
{
[super willTransitionToState:state];
if (state == UITableViewCellStateDefaultMask) {
self.isShowingDeleteConfirmationMask = NO;
} else if (state == UITableViewCellStateShowingDeleteConfirmationMask) {
self.isShowingDeleteConfirmationMask = YES;
}
}
- (void)refreshCellState
{
double delayInSeconds = 1.0; // one second should be enough for animationt to complete
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
BOOL deleteViewExists = NO;
for (UIView *subview in self.subviews) {
// There is a iOS bug where swiping left on cell to show delete button would not trigger default state
// if you don't swipe pass delete button
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
deleteViewExists = YES;
break;
}
}
if (!deleteViewExists && [self isEditing] && self.isShowingDeleteConfirmationMask) {
if ([self.delegate respondsToSelector:@selector(cellDidEndEditing:)]) {
[self.delegate cellDidEndEditing:self];
}
}
});
}
在你的 View Controller 中,
- (void)cellDidEndEditing:(nonnull LPToteItemTableViewCell *)cell
{
[self.tableView setEditing:NO];
}
关于ios - 夹在 willBeginEditingRowAtIndexPath 和 didEndEditingRowAtIndexPath 之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43128917/