简体   繁体   English

是否可以从同一视图将视图切换到表视图?

[英]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. 我有一个viewcontroller.xib,其中包含视图,按钮,工具栏按钮,文本框和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. 现在,当我单击工具栏上的按钮说“ viewtable”时,我希望视图移到tableview。 I have filled my tableview data with some default objects like this: 我已经用一些默认对象填充了tableview数据,如下所示:

- (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? 是否可以在单个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. 如果我创建另一个xib并将控制权简单地转移到该xib,则可以实现结果,但是我想知道是否有可能在不创建第二个xib文件的情况下做到这一点。 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: 在您的界面中获取tableView的句柄,如下所示:

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

  2. Hide your table view in initially ( like in viewDidLoad method ) 最初在您的表视图中隐藏(例如在viewDidLoad方法中)

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

  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; -(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. 您可以使用hidden属性来实现。 Put these in the appropriate ibaction methods. 将它们放在适当的方法中。

_tableView.hidden = Yes;

_tableView.hidden = No;

I'd highly recommend to do this in two separate XIBs. 我强烈建议在两个单独的XIB中执行此操作。 The first should contain a UIViewController (your initial view) and the second a UITableViewController (your table view) class. 第一个应该包含一个UIViewController (您的初始视图),第二个应该包含一个UITableViewController (您的表视图)类。 Both should be handled by a UINavigationController - don't fight the API and try your own hacks if it's not necessary. 两者都应该由UINavigationController处理-不要争吵API,如果没有必要,请尝试自己动手。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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