ios - 在 iOS 的表格 View 中隐藏单元格的标签

标签 ios uitableview label hide cell

我想在表格 View 中隐藏单元格的标签。

(void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
    //Get location of the swipe
    CGPoint location = [gestureRecognizer locationInView:self.tableView];

    //Get the corresponding index path within the table view
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];

    //Check if index path is valid
    if(indexPath)
    {
        //Get the cell out of the table view
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        //Update the cell or model
        displayLabel.hidden = TRUE;
        [cell setNeedsDisplay];
     }
 }

此代码将标签隐藏在最后一个单元格中,因为我未能指定用于隐藏滑动单元格标签的代码。

帮助指定要隐藏的滑动单元格标签。

displayLabel.hidden = TRUE;

我需要替换此代码。

最佳答案

你可以尝试下面的代码,它在我这边工作得很好:

- (void)viewDidLoad
    {
          UISwipeGestureRecognizer *recog = [[UISwipeGestureRecognizer alloc]initWithTarget:self
         action:@selector(handleSwipeRight:)];
          recog.delegate = self;
          [recog setDirection:UISwipeGestureRecognizerDirectionRight];

          [testTable addGestureRecognizer:recog];
         // add the swipe gesture recognizer to tableview;


          [super viewDidLoad];

}
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
   {

       return 7;
    }
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  
 *)indexPath
  {
    static NSString *CellIdentifier = @"Cell";    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

     if (cell == nil)
      {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
             reuseIdentifier:CellIdentifier] autorelease];
       UILabel *aLabel = [[[UILabel alloc]init]autorelease];
       aLabel.frame = CGRectMake(5, 0, 100, 40);
       aLabel.text = [NSString stringWithFormat:@"aLabel %d",indexPath.row+1];
       aLabel.tag = 1;//tag the labels
       [cell.contentView addSubview:aLabel];

       UILabel *bLabel = [[[UILabel alloc]init]autorelease];
       bLabel.frame = CGRectMake(110, 0, 100, 40);
       bLabel.text = [NSString stringWithFormat:@"bLabel %d",indexPath.row+1];
       bLabel.tag = 2;//tag the label
       [cell.contentView addSubview:bLabel];

       UILabel *cLabel = [[[UILabel alloc]init]autorelease];
       cLabel.frame = CGRectMake(215, 0, 100, 40);
       cLabel.text = [NSString stringWithFormat:@"cLabel %d",indexPath.row+1];
       cLabel.tag = 3;//tag the label
       [cell.contentView addSubview:cLabel];


      }



      return cell;
    }

    -(void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
      {
        //Get location of the swipe
        CGPoint location = [gestureRecognizer locationInView:testTable];

       //Get the corresponding index path within the table view
       NSIndexPath *indexPath = [testTable indexPathForRowAtPoint:location];

       //Check if index path is valid
        if(indexPath)
          {
            //Get the cell out of the table view
            UITableViewCell *cell = [testTable cellForRowAtIndexPath:indexPath];
           for (id label in  cell.contentView.subviews)
             {
                if ([label isMemberOfClass:[UILabel class]]) 
                  {
                    UILabel *referedLabel = (UILabel*)label;
                    if (referedLabel.tag == 2) //tag of bLabel;
                      {
                        referedLabel.hidden = YES;

                       }
                    }
                }
             }
          }

关于ios - 在 iOS 的表格 View 中隐藏单元格的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13453566/

相关文章:

r - 有没有办法在 R 中为每个顶点分配多个标签

java - JOGL 中的绘图标签

ios - 如何使 UIScrollView 忽略单次触摸?

ios - 为什么我的WKWebviews在模拟器上可以运行,但在iPad上却无法拉出文件?

ios - 添加到 View 时,约束项必须是该 View (或 View 本身)的后代。

ios7 navigationController pushViewController动画错误

iphone - 如何将后置摄像头实时视频显示到 UIImageView?

iphone - 分析时在 iPhone 应用程序中检测到潜在泄漏

ios - 使用字符串数组填充 UI TableView

javascript - 输入中不引人注目的 "default"文本,没有 jQuery