Saurabh Wadhwa
Saurabh Wadhwa

Reputation: 1173

How to display and hide table on button click in iOS?

I have a view controller. In view there is button "show/hide". When I click on this button table should display and when I click on this button again table should hide.

I'm using following code:

-(void)imageTapped:(UIButton *)sender
{
    buttonclk=TRUE;
    if (buttonclk==TRUE)
    {
        [self addTableView];
        buttonclk=FALSE;
    }
    else
    {
        tableView1.hidden=YES;
    }
}

-(void)addTableView
{
    CGRect fr = CGRectMake(0,176,320,500);

    tableView1 = [[UITableView alloc] initWithFrame:fr style:
                  UITableViewStylePlain];
    tableView1.autoresizingMask = UIViewAutoresizingFlexibleHeight|
                                  UIViewAutoresizingFlexibleWidth;
    tableView1.delegate = self;
    tableView1.dataSource = self;
    tableView1.separatorColor = [UIColor darkGrayColor];
    [self.view addSubview:tableView1];
}

When I use this code table is displaying but not hiding.

Upvotes: 0

Views: 2222

Answers (2)

Lukas Treml
Lukas Treml

Reputation: 31

-(void)buttonClicked:(UIButton *)sender {
        //toggle style
        tableView1.hidden=!tableView1.hidden; 
}

Upvotes: 0

AnjDenny
AnjDenny

Reputation: 468

-(void)imageTapped:(UIButton *)sender {
    if (tableView1.hidden)    {
       tableView1.hidden=NO;
    }
    else
    {
        tableView1.hidden=YES;
    } 
}

Upvotes: 1

Related Questions