开发者

Embedding two UITableViewControllers in one UIViewController

开发者 https://www.devze.com 2023-04-10 19:39 出处:网络
I have a design where I need to swap in and out two tableviews using one viewcontroller (I need the same navigation title to appear for both). So I\'ve created each tableview in its own subclassed tab

I have a design where I need to swap in and out two tableviews using one viewcontroller (I need the same navigation title to appear for both). So I've created each tableview in its own subclassed tableviewcontroller class, then I've included a class variable for each in my viewcontroller. Each tableview has the viewcontroller as a parent and calls the viewcontrollers swap method when a swap needs to occur. This method and the viewDidLoad method is listed below:

- (void) viewDidLoad 
{
    [super viewDidLoad];

    [[se开发者_JAVA技巧lf navigationItem] setTitle: AddAPhotoViewControllerTitle];

    SelectAnAlbumTableViewController *selectAnAlbumTableViewControllerTemp = [[SelectAnAlbumTableViewController alloc] initWithParent: self];
    [self setSelectAnAlbumTableViewController: selectAnAlbumTableViewControllerTemp];
    [selectAnAlbumTableViewControllerTemp release];

    [[self view] insertSubview: [[self selectAnAlbumTableViewController] tableView] atIndex: 0];
}

- (void) switchTableViews
{
    if ([[[self selectAnAlbumTableViewController] tableView] superview] == nil)
    {


        [[self view] insertSubview: [[self selectAnAlbumTableViewController] tableView] atIndex: 0];


             [[[self selectAPhotoTableViewController] tableView] removeFromSuperview];
             [selectAPhotoTableViewController release];
             selectAPhotoTableViewController = nil;
    }
    else 
    {
        SelectAPhotoTableViewController *selectAPhotoTableViewControllerTemp = [[SelectAPhotoTableViewController alloc] initWithAssetGroup: [[self selectAnAlbumTableViewController] assetGroup] parent: self];
        [self setSelectAPhotoTableViewController: selectAPhotoTableViewControllerTemp];
        [selectAPhotoTableViewControllerTemp release];

        [[self view] insertSubview: [[self selectAPhotoTableViewController] tableView] atIndex: 0];

            [[[self selectAnAlbumTableViewController] tableView] removeFromSuperview];

    }

I have 2 questions:

1) When I'm doing the swap, I'm inserting the new tableview, animating the transition (I didn't include the animation code to keep it concise), then removing the old tableview from the superview. Is this the correct order / correct way to do this? It works fine, but I'm wondering if there isn't some code smell here.

2) Using this design pattern, what would be the best way to go about putting in an activity indicator that can be displayed while each tableview is loading? I've tried implementing the indicator in the viewcontroller, and it seemed to work ok, but I wasn't sure how to set it's position? In terms of what? The center of? I guess this goes back to the first question I asked to, what is the superview and when, or if there is even a superview?


Just use two UITableViews.

Instantiate them and put them in instance variables. If you need to, save the state i.e. which table view is being displayed.


I see nothing wrong with it. I assume your reason is that each table controller has its own logic as to deserve a separate class. It is a bit unorthodox to embed controllers inside controllers, and you'll have to pass the view lifecycle calls.

Example:

-(void) switch
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationDelegate:self];

    if ([self.visibleVC isKindOfClass:[OrangeVC class]])
    {
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
        [self.visibleVC viewWillDisappear:TRUE];
        [self.visibleVC.view removeFromSuperview];
        self.visibleVC = self.appleVC;
        [self.view addSubview:self.visibleVC.view];
    }
    else if ([self.visibleVC isKindOfClass:[AppleVC class]])
    {
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
        [self.visibleVC viewWillDisappear:TRUE];        
        [self.visibleVC.view removeFromSuperview];
        self.visibleVC = self.orangeVC;
        [self.view addSubview:self.visibleVC.view];
    } 
    [self.visibleVC viewWillAppear:TRUE];
    [UIView commitAnimations];
}

Note the viewWillAppear calls, you need at least that to wake up the table. I'm not sure if you should call viewWillDisappear, but it doesn't hurt. If you don't, watch it in instruments in case the controller is not cleaning up properly.

You can add any activity hud in the self.view of the parent controller, use a hud widget, darken the view currently loading, ..., it's a design issue more than technical. Remember to disable the user interaction while you load (userInteractionEnabled=NO) on the parent view and the button that initiated the switch.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号