2

我有一个自定义的UITableview,它被划分为几个部分,我已经实现了UISwipeGestureRecognizer。当我滑动我的表格视图单元格时,出现UIButton。我现在面临的问题是当我滑动第一个表格视图单元格时,连续部分中的另一个单元格也会识别滑动手势。我无法找到如何在没有其他部分的其他单元格被滑动的情况下轻扫特定部分的单元格。如何在一个部分的特定行中滑动tableviewcell

我只想刷卡,我不想删除/插入或添加复选标记。

UPDATE ....这是我customCell.m 泰伯维= customTableView

 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
     { 
      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
      if (self) 
      { 
       self.backgroundColor = [UIColor clearColor]; 
       self.contentView.backgroundColor = [UIColor clearColor]; 

       self.symptomCellImageView.contentMode=UIViewContentModeScaleToFill; 


       swipeButton = [[UIButton alloc]init]; 
       swipeButton .frame = CGRectMake(220.0, 8.0, 30.0, 30.0); 
       swipeButton .hidden = NO; 

} 

在我MainViewController.m

-(void)viewDidLoad 
    {   

     symptomSwipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandleLeft)]; 
     symptomSwipeLeft .numberOfTouchesRequired = 1; 
     symptomSwipeLeft .direction = UISwipeGestureRecognizerDirectionLeft; 
     [customTableView addGestureRecognizer:symptomSwipeLeft]; 
     [self addGestureRecognizer:symptomSwipeRight]; 

      symptomSwipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandleRight)]; 
      symptomSwipeRight.numberOfTouchesRequired = 1; 
      symptomSwipeRight.direction = UISwipeGestureRecognizerDirectionRight; 
      [customTableView addGestureRecognizer:symptomSwipeRight]; 
      [self addGestureRecognizer:symptomSwipeRight]; 

    } 


- (void)swipeHandleLeft:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer 
{ 
    CGPoint location = [aSwipeGestureRecognizer locationInView:customTableView ]; 
    NSIndexPath * indexPath = [customTableView indexPathForRowAtPoint:location]; 

    if(indexPath) 
{ 
     UITableViewCell * cell = (UITableViewCell *)[customTableView cellForRowAtIndexPath:indexPath]; 
     [cell.swipeButton addTarget:self action:@selector(secondPageButton:) forControlEvents:UIControlEventTouchUpInside]; 

       } 
} 



- (void)swipeHandleRight:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer 
{ 
    CGPoint location = [aSwipeGestureRecognizer locationInView:customTableView ]; 
    NSIndexPath * indexPath = [customTableViewindexPathForRowAtPoint:location]; 

    if(indexPath) 
{ 
     UITableViewCell * cell = (UITableViewCell *)[customTableView cellForRowAtIndexPath:indexPath]; 
     [cell.swipeButton addTarget:self action:@selector(secondPageButton:) forControlEvents:UIControlEventTouchUpInside]; 
       } 
} 

回答

3

尝试:

- (void)handleSwipe:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer; { 
    CGPoint location = [aSwipeGestureRecognizer locationInView:_tableView]; 
    NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location]; 

    if(indexPath){ 
    UITableViewCell * cell = (UITableViewCell *)[_tableView cellForRowAtIndexPath:indexPath]; 
    [cell whateverMethodYouWant]; 
    } 
} 

作为一个侧面说明,您正在调用多个单元的原因是因为该行不够独特。在你们所有的部分都有row = 0。因此,如果你想每个单元都有一个唯一的号码连接到其.tag财产,你需要知道的任何部分的最大行数(称之为largestNumberOfRows),然后计算:

cell.swipeButton.tag = (indexPath.section * largestNumberOfRows) + indexPath.row;

希望有所帮助!

编辑:

使用上述方法,在你viewDidLoad;方法,添加以下代码:

UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft)]; 
[_tableView addGestureRecognizer:recognizer]; 

EDIT 2

看着你代码,你已经把你的手势识别器放在了错误的文件。下面是设置:

在你MainViewController.m文件:

- (void)viewDidLoad; { 
    [super viewDidLoad]; 
    UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft)]; 
    [_tableView addGestureRecognizer:recognizer]; 
} 

- (void)handleSwipe:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer; { 
    CGPoint location = [aSwipeGestureRecognizer locationInView:_tableView]; 
    NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location]; 

    if(indexPath){ 
    UITableViewCell * cell = (UITableViewCell *)[_tableView cellForRowAtIndexPath:indexPath]; 

    if(aSwipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionRight){ 
     [cell symptomCellSwipeRight]; 
    } 
    else if(aSwipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionLeft){ 
     [cell symptomCellSwipeLeft]; 
    } 
    } 
} 

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; { 
    // Initial your cell. Then add: 
    [cell.swipeButton addTarget:self action:@selector(secondPageButton:) forControlEvents:UIControlEventTouchUpInside]; 
} 

在你PPsymptomTableCell.m文件:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier; { 
    if((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])){ 
    self.backgroundColor = [UIColor clearColor]; 
    self.contentView.backgroundColor = [UIColor clearColor]; 

    self.symptomCellImageView.contentMode=UIViewContentModeScaleToFill; 

    // SHARE ON FACEBOOK BUTTON // 

    shareFacebookButton = [[UIButton alloc]init]; 
    shareFacebookButton.frame = CGRectMake(220.0, 8.0, 30.0, 30.0); 
    shareFacebookButton.hidden = NO; 

    // DISPLAY NOTIFICATION IMAGE // 

    selectedCellImageDisplay = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"selectedSymptomImage.png"]]; 
    selectedCellImageDisplay.frame = CGRectMake(240.0, 8.0, 30.0, 30.0); 

    // SYMPTOM NAME // 

    symptomCellLabel=[[UILabel alloc]initWithFrame:CGRectMake(15.0,0.0 ,280.0,40.0)]; 
    symptomCellLabel.font=[UIFont fontWithName:@"Rockwell" size:17]; 
    symptomCellLabel.textColor=[UIColor blackColor]; 
    symptomCellLabel.backgroundColor=[UIColor clearColor]; 

    [self.contentView addSubview:symptomCellLabel]; 
    [self.contentView addSubview:selectedCellImageDisplay]; 
    [self.contentView addSubview:shareFacebookButton]; 
    } 
    return self; 
} 

- (void)symptomCellSwipeLeft; { 
    [UIView beginAnimations:@"HideView" context:nil]; 
    symptomCellLabel.frame = CGRectMake(-183.0, 0.0, 280.0, 40.0); 
    selectedCellImageDisplay.frame = CGRectMake(-340.0, 8.0, 30.0, 30.0); 
    shareFacebookButton.frame = CGRectMake(120.0, 8.0, 80.0, 30.0); 
    shareFacebookButton.hidden = NO; 
    shareFacebookButton.backgroundColor = [UIColor redColor]; 

    [UIView setAnimationDuration:0.3f]; 
    [UIView commitAnimations]; 
} 


- (void)symptomCellSwipeRight; { 
    [UIView beginAnimations:@"HideView" context:nil]; 
    symptomCellLabel.frame = CGRectMake(15.0,0.0 ,280.0,40.0); 
    selectedCellImageDisplay.frame = CGRectMake(240.0, 8.0, 30.0, 30.0); 
    shareFacebookButton.frame = CGRectMake(220.0, 8.0, 30.0, 30.0); 
    shareFacebookButton.hidden = YES; 

    [UIView setAnimationDuration:0.3f]; 
    [UIView commitAnimations]; 
} 

这应该得到的一切工作很好。

+0

..不应该叫[单元格。swipeButton addTarget:自我行动:@选择(intoView :) forControlEvents:UIControlEventTouchUpInside]中的cellForRowAtIndexPath 和标签应的cellForRowAtIndexPath做正确的 – raptor 2013-04-22 17:15:11

+0

我的代码将你的'UISwipeGestureRecognizer',给你的用户刷卡确切的细胞。从那里,你可以调用你喜欢的任何方法。因此,如果你想要出现滑动按钮,只需调用'cell.swipeButton.hidden =!cell.swipeButton.hidden'(这将使按钮隐藏,如果它隐藏或反之亦然)。你甚至可以调用一个自定义的单元格方法来使按钮变成动画!根本不需要'.tag'属性! – msgambel 2013-04-22 17:21:57

+0

谢谢:)...我会研究它..只是再一件事情,如果我在第一节中刷单元格,然后单元格在其他部分不会被刷卡正确 – raptor 2013-04-22 17:23:20

0

您正在使用sections执行您的表格,然后将您的标记值组合为indexPath.sectionsindexPath.row

但请确保您实现的tagValue不重叠。

相关问题