iOS滑动检测仅适用于1个UITableViewCell?

时间:2013-12-02 08:21:01

标签: ios objective-c uitableview uiswipegesturerecognizer

我试图在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!");
}

任何人都知道为什么滑动检测仅适用于其中一个细胞?

2 个答案:

答案 0 :(得分:2)

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");
    }

答案 1 :(得分:1)

您最好在CustomPlaceCell's方法中添加手势:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier.

另外,你最好这样做:

[cell.contentView addGestureRecognizer:]

,而不是细胞本身。如果您在可重用单元格中执行更复杂的行为,您将发现许多好处。