如何获得UITableViewCell移动开始和结束的通知



我的iOS应用程序中有一个UITableView,它会定期刷新。用户还可以随时移动表视图行(表视图始终处于编辑模式)。

我想在用户开始拖动行时停止刷新计时器,并在删除行时重新启动它。

最后一部分应该很容易使用moveRowAtIndexPath,但如何获得关于拖动启动的通知?

UITableViewDelegate将收到以下通知以响应重新排序操作:

- (void)tableView:(UITableView *)tableView willBeginReorderingRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didEndReorderingRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didCancelReorderingRowAtIndexPath:(NSIndexPath *)indexPath;

我不久前遇到了同样的问题,但没有找到解决方案。当我以解释为什么不能做到这一点开始回答时,我实际上发现了它是如何做到的!:-)

简而言之:您必须创建UITableViewCell的自定义子类。覆盖layoutSubviews以将UILongPressGestureRecognizer附加到UITableViewCellReorderControl。定义一个协议,并使用委托来通知任何想要了解拖动状态的人。

CustomTableViewCell.h:

#import <UIKit/UIKit.h>
@protocol CustomTableViewCellDelegate;
@interface CustomTableViewCell : UITableViewCell {
}
@property (nonatomic, assign) id <CustomTableViewCellDelegate> delegate;
@end
@protocol CustomTableViewCellDelegate
- (void)CustomTableViewCell:(CustomTableViewCell *)cell isDragging:(BOOL)value;
@end

CustomTableViewCell.m:

#import "CustomTableViewCell.h"
@implementation CustomTableViewCell
@synthesize delegate = _delegate;
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        [_delegate CustomTableViewCell:self isDragging:YES];    // Dragging started
    } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        [_delegate CustomTableViewCell:self isDragging:NO];     // Dragging ended
    }
}
- (void)layoutSubviews {
    [super layoutSubviews];
    for (UIView *view in self.subviews) {
        if ([NSStringFromClass ([view class]) rangeOfString:@"ReorderControl"].location != NSNotFound) {    // UITableViewCellReorderControl
            if (view.gestureRecognizers.count == 0) {
                UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
                gesture.cancelsTouchesInView    = NO;
                gesture.minimumPressDuration    = 0.150;
                [view addGestureRecognizer:gesture];
            }
        }
    }
}
@end

请注意,虽然此代码不使用任何私有API,但如果苹果更改其内部实现(即更改UITableViewCellReorderControl的类名),它仍可能停止工作。

我刚刚发现了一个破解方法,由于苹果会减少alpha,我们可以使用它,我猜

@interface CustomTableViewCell () 
@property (nonatomic, assign) BOOL isDragging;
@end
-(void)draggingWillBegan
{
    //use a delegate method to inform tableview
}
-(void)draggingDidEnd
{
    //use a delegate method to inform tableview
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    //Since apple wont provide en
    if(self.alpha <= 0.95 && !self.isDragging){
        self.isDragging = YES;
        [self draggingWillBegan];
    }
    if(self.alpha >= 0.95 && self.isDragging){
        self.isDragging = NO;
        [self draggingDidEnd];
    }
}

当你移动完单元格时,会调用这个方法:

- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

最新更新