我正在尝试在2个表格单元格上添加滑动检测。但是,到目前为止,滑动检测仅适用于一个表格单元。

这是我的代码的一部分:

- (void)viewDidLoad {

     [super viewDidLoad];
     numbers = [[NSMutableArray alloc]init];
     tableCellTrash = [[NSMutableArray alloc]init];

     mSwipeRecognizer= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(removeCell:)];
     [mSwipeRecognizer setDirection:( UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CustomPlaceCell";
    CustomPlaceCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomPlaceCell" owner:nil options:nil] objectAtIndex:0];
        [numbers addObject:cell];
        [cell addGestureRecognizer:mSwipeRecognizer];
        NSLog(@"Cell");
    }

    return cell;
}

-(void)removeCell:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer{
    NSLog(@"Swipe Detected!");
}

有人知道为什么滑动检测仅适用于其中一个单元吗?

最佳答案

UIGestureRecognizer只能与单个视图关联。您应该为每个单元格设置一个不同的gestureRecognizer。您可以将GestureRecognizer创建的内容移到

     if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomPlaceCell" owner:nil options:nil] objectAtIndex:0];
        [numbers addObject:cell];

        UISwipeGestureRecognizer *swipeRecognizer= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(removeCell:)];
        [swipeRecognizer setDirection:( UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];

        [cell addGestureRecognizer:swipeRecognizer];

        NSLog(@"Cell");
    }

关于ios - iOS滑动检测仅适用于1个UITableViewCell?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20323662/

10-12 04:12