0

I'm working on the TableView, and I have used the label on the TableViewCell and on the button click I want to hide the label from all cells in my table, In the label I have set the tag:

label.tag = indexPath.row+1;

And on the button click I am using the code like this:

for (int i = 0; i < Array.count; i++)
{
    [[self.view viewWithTag:i+1] setHidden:YES];      
}

But from my code the label is hiding only from the last cell not all the others.

Navnath Godse
  • 2,233
  • 2
  • 23
  • 32

4 Answers4

1

You can simply do this with another way.

  1. First you need to declare a BOOL in your class

    @property(assign,nonatomic) BOOL hideLabels;
    
  2. Next in your button action handler method, set this YES

  3. In your cellForRowAtIndexPath, check whether hideLabels is YES, if yes, the hide labels using code.

     cell.yourLabel.hidden = hideLabels;
    
  4. Now reload the table after setting hideLabels as YES

    [self.tableView reloadData];
    
manujmv
  • 6,450
  • 1
  • 22
  • 35
0
 for(id object in tableView.subviews)
    {

      if([object isKindOfClass:[UILabel class]])
      {
        UILabel *label = (UILabel *) object;
        [[label setHidden:YES];      

      }
  }
Samkit Jain
  • 2,523
  • 16
  • 33
0

In your ViewController.h

BOOL isLabelHidden;

in ViewController.m

- (void)viewDidLoad
{
   isLabelHidden = FALSE;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"TableCell";

    UILabel *lbl;
    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = nil;
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

     if(isLabelHidden)
       [lbl setHidden:YES];
     else
       [lbl setHidden:NO];
}

in you button clicked method

- (void)buttonClicked
{
  isLabelHidden = TRUE;
  [tableView reloadData];
}
DipakSonara
  • 2,598
  • 3
  • 29
  • 34
0

You should first get the reference to the UITableViewCell and then you can remove the labels in them.

First of all get the reference to all the cells in your tableview as follows:

NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < [tableView numberOfSections]; ++j)
{
for (NSInteger i = 0; i < [tableView numberOfRowsInSection:j]; ++i)
{
    [cells addObject:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
}
}

And now iterate through those cells in cells Array to hide the label view,

 for (int i = 0; i < cells.count; i++)
    {
       [[[cells objectAtIndex:i] viewWithTag:i+1] setHidden:YES];      
    }

Source:How can I loop through UITableView's cells?

Community
  • 1
  • 1
Bikram Thapa
  • 1,329
  • 1
  • 16
  • 29