UITableView segue within the same ViewController

半世苍凉 提交于 2019-12-08 09:03:15

问题


I have a UIViewController that contains both, a map and a tableView which are connected together and it looks something like this:

And I'd like to be able to push the tableView to another tableView within the same view controller so that the map stays where is it. Of course I'd like to be able to unwind the segue backwards programmatically with a button that'll appear in the navbaritem in the top left.

Similar behaviour can be observed in the iPad tableView controllers placed in a popover. How would I go about doing this in Swift (iOS 8) ?

Any code snippets or pointers to existing code examples are greatly appreciated.


回答1:


You can achieve view controller containment like so:

- (void)viewDidLoad {
        [super viewDidLoad];

        // Setup the container view
        _containerView = [[UIView alloc] initWithFrame: // DESIRED FRAME];
        _containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:_containerView];

        // Setup the main view controller
        [self addChildViewController:_mainViewController];
        _mainViewController.view.frame = _containerView.bounds;
        [_containerView addSubview:_mainViewController.view];
        [_mainViewController didMoveToParentViewController:self];
}

Now you can create both table views in separate view controllers. It sounds like you should add a navigation controller ("mainViewController" in code snippet) to your main view controller so you can "push" a new table onto the stack.




回答2:


This technically is not a "push" -- when the user selects a cell on your tableview, triggering a call to the tableview delegate method

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

...you should bring in a second table view from the right side of the screen.

Each of your tableview datasource and delegate methods should look distinguish between the first and second tableview. Here's an example for cellForRowAtIndexPath:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == self.tableView1 {
        return self.collection1.count
    } else if tableView == self.tableView2 {
        return self.collection2.count
    }
    return 0;
}



回答3:


I think you should use the Basic UIView Animation to show the seconds table moving from left side of screen on didSelectRowAtIndexPath for the first table



来源:https://stackoverflow.com/questions/28705530/uitableview-segue-within-the-same-viewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!