0

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

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

我需要替换此代码。

4

3 回答 3

3

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

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

                       }
                    }
                }
             }
          }
于 2012-11-19T12:58:32.783 回答
0

我认为 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];
于 2012-11-19T12:28:48.033 回答
0

将您的方法更改为:

-(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];
    }
}
于 2012-11-19T12:41:49.483 回答