在tableView中,只需轻扫一下单元格即可触发willBeginEditingRowAtIndexPath
进入编辑模式。手势向左拉的时间不足以使单元格停留在左侧,同时暴露了3个UITableViewRowAction
按钮。而是,单元格滑回到右侧。但是,didEndEditingRowAtIndexPath
不会自动触发。只有随后的触摸手势将触发该方法。如果单元格向右滑动,为什么didEndEditingRowAtIndexPath
不触发?当手势结束和/或单元格返回到右侧对齐的正常状态时,我该如何拾取?
或换一种说法。 willBeginEditingRowAtIndexPath
被解雇,并将屏幕状态更改为编辑状态。但是,由于从未达到编辑阶段,因此我需要将屏幕状态更改回非编辑状态。 didEndEditingRowAtIndexPath
可以做到这一点,但是直到再次触摸它才被调用,这会使用户感到困惑。是否还有另一个指标,也许在UIControl
中可以触发我使用?
更多信息:UITableViewRowAction按钮基于以下回购被子类化: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];
}
}
});
}
在您的视图控制器中,
- (void)cellDidEndEditing:(nonnull LPToteItemTableViewCell *)cell
{
[self.tableView setEditing:NO];
}