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

Posted

技术标签:

【中文标题】如何在部分中为特定行滑动 tableviewcell【英文标题】:How to swipe tableviewcell for particular row in a section 【发布时间】:2013-04-22 17:02:06 【问题描述】:

我有一个自定义的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];
              
 

【问题讨论】:

【参考方案1】:

试试:

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

这应该可以让一切正常运行。

【讨论】:

我的代码将获取您的UISwipeGestureRecognizer,并为您提供用户正在滑动的确切单元格。从那里,您可以调用任何您喜欢的方法。因此,如果您希望显示滑动按钮,只需调用cell.swipeButton.hidden = !cell.swipeButton.hidden(如果按钮被隐藏,这将使按钮取消隐藏,反之亦然)。您甚至可以调用自定义单元格方法来使按钮动画化!根本不需要.tag 属性! 谢谢 :)... 我会调查一下 .. 如果我在第 1 部分中滑动一个单元格,只需再做一件事,那么其他部分中的单元格不会被正确滑动 不,我给你的代码只会选择一个单元格,所以其他单元格都不会改变。 是的,但这是另一个问题。【参考方案2】:

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

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

【讨论】:

以上是关于如何在部分中为特定行滑动 tableviewcell的主要内容,如果未能解决你的问题,请参考以下文章

在swift 3中的tableview中更新tableviewcell中的特定行?

如何在 SVG 中为特定部分制作透明颜色代码

如何在D3中为文本div的特定部分着色?

如何仅在 UIButton 中为标题的特定部分设置图像

在 IntelliJ 中为一行禁用警告

滑动单元格时,有没有办法从 tableViewCell 导航到另一个视图控制器?