简体   繁体   中英

Is it possible to switch view to tableview from same view?

I have a viewcontroller.xib which contains View, buttons,toolbarbutton, text box and a tableview. When I load the initial screen comes without table view which is fine. Now when I click on a toolbarbutton say, viewtable, I want the view to move to tableview. I have filled my tableview data with some default objects like this:

- (void)viewDidLoad
{
       tableData = [[NSArray alloc] initWithObjects:@"object1",@"object2",@"object3",@"object4", nil];

    [super viewDidLoad];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:@"My Cell"];

    if(cell==nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"My Cell"];
    }
    cell.textLabel.text=[tableData objectAtIndex:indexPath.row];
    return cell;
}

So when I click on toolbar view button it should show tableview with toolbar button which also has a back button so when I click on that it should hide the table view and show the initial view. Is it possible to do all this in single xib? I can achieve the result if I create another xib and simply transfer control over to that xib but I wanted to know if its possible do this without creating a second xib file. And also for navigation I can use navigation controller but I want to check and see if its possible to use toolbar to transfer the control. Thanks.

If you don't need animation then you can do the following

  1. Get a handle of tableView in your interface like this:

    @property(nonatomic,assign)IBOutlet UITableView *tableView;

  2. Hide your table view in initially ( like in viewDidLoad method )

    -(void)viewDidLoad { [super viewDidLoad]; self.tableView.hidden = YES; }

  3. Then in the method called by your toolbar's button do the following

    -(void)on_click_toolbar_button { self.tableView.hidden = !self.tableView.hidden; //This will keep toggling the table view from hidden to shown & vice-versa. }

you could use the hidden property to achieve that. Put these in the appropriate ibaction methods.

_tableView.hidden = Yes;

_tableView.hidden = No;

I'd highly recommend to do this in two separate XIBs. The first should contain a UIViewController (your initial view) and the second a UITableViewController (your table view) class. Both should be handled by a UINavigationController - don't fight the API and try your own hacks if it's not necessary. The mentioned controller classes give you everything you need out of the box.

好吧,不建议这样做,但是您可以通过删除并添加tableview来实现。

First check if your table view is inside your view, if not put it inside and set delegate of datasource to file owner, then in your view table method write this code

-(void)viewTable
{
self.tableView.hidden = NO;
self.viewToolbar.hidden=YES;
}

On your back button code in toolbar write

-(void)goback
{   
self.tableView.hidden = YES;
self.viewToolbar.hidden=NO;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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