如何在部分中的特定行滑动tableviewcell

时间:2013-04-22 17:02:07

标签: iphone ios objective-c uitableview uiswipegesturerecognizer

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

我只是想滑动,我不想删除/插入或添加复选标记。

更新 ....这是我的 customCell.m  Tableview = 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];
              }
 }

2 个答案:

答案 0 :(得分: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),然后计算:< / p>

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];

编辑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];
}

这应该可以使一切运转良好。

答案 1 :(得分:0)

您正在使用sections实施您的表格,然后将您的代码值设为indexPath.sectionsindexPath.row的组合。

但请确保您在不重叠tagValue的情况下实施。