简体   繁体   English

在iOS的表格视图中隐藏单元格的标签

[英]Hide a label of a cell in a table view in iOS

I want to hide a label of a cell in a tableview. 我想在表格视图中隐藏单元格的标签。

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

This code is hiding the label in the last cell as I failed to specify the code to hide the swiped cell's label. 由于我未能指定代码来隐藏滑动单元格的标签,因此此代码将标签隐藏在最后一个单元格中。

Help to specify the swiped cell label to hide. 帮助指定要隐藏的滑动单元格标签。

displayLabel.hidden = TRUE;

I need a replacement for this code. 我需要替换此代码。

You can try below code its working perfectly on my side: 您可以尝试以下代码使其在我这方面工作得很完美:

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

                       }
                    }
                }
             }
          }

I think displayLabel is pointing to label in last cell of your table. 我认为displayLabel指向表的最后一个单元格中的标签。 Where you are setting value to displayLabel. 您将值设置为displayLabel的位置。

     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
     //Update the cell or model
     //You should get label from cell here. Currently displayLabel may be pointing to label in last cell, if I am correct you are assigning value to displaylabel in cellForRowAtIndexPath or in any other method.

     displayLabel.hidden = TRUE;
     [cell setNeedsDisplay];

Change your method to this: 将您的方法更改为此:

-(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)
    {
        // If you created a custom cell with a 
        // displayLabel property, change the pointer
        // type from UITableViewCell to that type
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.displayLabel.hidden = YES;

        // Use the following three lines instead, if your cell style is "Subtitle"
        //        cell.textLabel.hidden = YES;
        //        cell.imageView.hidden = YES;
        //        cell.detailTextLabel.hidden = YES;

        [cell setNeedsDisplay];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM