Muhammad Zaheer
Muhammad Zaheer

Reputation: 123

Hide Label from prototype cell in TableView

I am working on Prototype TableView Cell where I have two Label over cell and one button behind the cell. I want to hide the second label on Hide Button click. I have tried a lot but did not get any appropriate info, Here is my code for displaying the Labels in Cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *simpleTableIdentifier = @"TableCell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

// Configure the cell...

int row = [indexPath row];
cell.EnglishLable.text = _English[row];
cell.UrduLable.text = _Urdu[row];
return cell;
}

Everything is fine, I just need to hide 'UrduLabel' in my Hide IBAction method.

- (IBAction)Hide:(id)sender {



static NSString *simpleTableIdentifier = @"TableCell";
UITableView *tableView = [[UITableView alloc]init];
CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:IndexPath];
BOOL *hideLabel;
hideLabel = YES;
[self.tableView reloadData];
UILabel *subtitle = (UILabel *)[cell.contentView viewWithTag:1];
subtitle.hidden = hideLabel;
}

Whats wrong in my Button method?

Upvotes: 2

Views: 1256

Answers (2)

Ratul Sharker
Ratul Sharker

Reputation: 8011

IN CASE ONE BUTTON IN THE VIEWCONTORLLER FOR ALL CELL

Incase of single button toggling the visibility of the UrduLabel, declare the -(IBAction)hide:(sender)id in the yourviewcontroller.m. connect the IBAction of the button from nib/storyboard to the -(IBAction)hide:(sender)id method.

Then follow the following steps

  1. declare a BOOL property variable or ivar inside the view controller. named willShowUrdu.

  2. implement the hide as following:

    -(IBAction)hide:(id)sender
    {
        willShowUrdu = !(willShowUrdu);
        [yourTableView reloadData];
    }
    
  3. inside the cellForRowAtIndexPath:

    TableViewCell *cell = //whatever the way you get the cell
    cell.UrduLabel.hidden = !willShowUrdu;
    

Thats how toggle the visibility of all UrduLabel under one IBAction.

IN CASE OF EACH BUTTON IN EACH CELL

There is a better way to de-centralize the control to each cell. Move the -(IBAction)hide:(sender)id method to the implementation in the TableViewCell's implementation.

  1. Add the IBAction from nib/storyboard to the moved -(IBAction)hide:(sender)id method.
  2. Take a IBOutlet of the UrduLabel, as seen in above code, it's already taken.

Then the implementation of -(IBAction)hide:(sender)id would be following:

-(IBAction)hide:(sender)id
{
    //put your other logic here (if any)
    self.UrduLabel.hidden = YES;
}

Welcome to IOS, Happy coding.

Upvotes: 2

Arun
Arun

Reputation: 1411

Try this code

- (IBAction)Hide:(id)sender {

CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];

TableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.UrduLable.hidden = YES;
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:<any row animation you need>];

UILabel *subtitle = (UILabel *)[cell.contentView viewWithTag:1];
subtitle.hidden = hideLabel;

}

You just need to get the particular cell of which the button is clicked, hide the required label and reload the particular cell. And main thing is you need to get an instance of your prototype cell class, not of UITableViewCell

Upvotes: 3

Related Questions