2

我有一个习惯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];
              }
 }
4

2 回答 2

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

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

这应该让一切正常工作。

于 2013-04-22T17:08:12.657 回答
0

您正在使用 实现您的表格sections,然后将您的标签值设置为indexPath.sections和的组合indexPath.row

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

于 2013-04-22T17:23:30.970 回答