0

I have a very long screen (a lot of fields). Structure in XCode:

View
  ScrollView
    View
       View
       .
       .
       .
       TableContainer (View)
         TableView

TableContainer and TableView have height 0. When I go on page - I set height of TableView height according to items count and increase TableView according to items count. All works OK. But, when I rotate device - TableView disappears (height resets to 0).

Class:

public partial class TrackingView: BaseView<TrackingViewModel>
    {
        public TrackingView() : base("TrackingView")
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            var source = new RetrieveShipmentDetailsViewSource(PortsTable, ShipmentDetailsCell.Key, ShipmentDetailsCell.Key);
            PortsTable.Source = source;

            var set = this.CreateBindingSet<TrackingModel, TrackingViewModel>();

            set.Bind(ShipmentLabel).To(vm => vm.ShipmentNumber);
            set.Bind(CustCodeLabel).To(vm => vm.Shipment.CustomerCode);
            set.Bind(CustNameLabel).To(vm => vm.Shipment.CustomerName);
        );

            ViewModel.ShipmentLoadedSuccess += (sender, e) =>
            {
                InvokeOnMainThread(delegate
                {
                    PortsTable.ReloadData();
                    UpdateView();
                });
            };

            PortsTable.SeparatorStyle = UITableViewCellSeparatorStyle.None;
            PortsTable.ReloadData();

            set.Apply();            
        }

        private void UpdateView()
        {
            if (ViewModel.Shipment != null && ViewModel.Shipment.VesselPorts != null)
            {
                PortsTable.ContentSize = new CGSize(MainView.ContentSize.Width, (ViewModel.Shipment.VesselPorts.Count *  200) + 55);
                PortsTable.Frame = new CGRect(PortsTable.Frame.X, PortsTable.Frame.Y, PortsTable.Frame.Size.Width,
                                              (ViewModel.Shipment.VesselPorts.Count *  200) + 55);
                MainView.ContentSize = new CGSize(MainView.ContentSize.Width, 
                                                  MainView.ContentSize.Height + (ViewModel.Shipment.VesselPorts.Count *  200) + 55);
            }
        }
    }

I'm new in iOS and I know that my structure isn't good. Advice, please, how to reload TableView and ScrollView. Help me, please.

DaleYY
  • 130
  • 1
  • 9

1 Answers1

1

In Xcode, Use this API to update your UI when rotate the device:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);

Implement it in Xcode with Objective-C like this:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    // Code here will execute before the rotation begins.
    // Equivalent to placing it in the deprecated method -[willRotateToInterfaceOrientation:duration:].
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        // Place code here to perform animations during the rotation.
        // You can pass nil for this closure if not necessary.
        // Reorganize views, or move child view controllers.
        if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {

            //Update UI
        }

        if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {

            //Update UI
        }

    }completion:^(id<UIViewControllerTransitionCoordinatorContext> context){
                                     // Code here will execute after the rotation has finished.
                                     // Equivalent to placing it in the deprecated method -[didRotateFromInterfaceOrientation:].
                                     // Do any cleanup, if necessary.

                                 }];
}

It also can be found in Xamarin.iOS, ViewWillTransitionToSize. And it can be implemented with C# like this:

    public override void ViewWillTransitionToSize(CGSize toSize, IUIViewControllerTransitionCoordinator coordinator)
    {
        base.ViewWillTransitionToSize(toSize, coordinator);

        coordinator.AnimateAlongsideTransition((IUIViewControllerTransitionCoordinatorContext) => {

            if (UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.Portrait || UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.PortraitUpsideDown)
            {
                //Update UI
            }

            if (UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.LandscapeLeft|| UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.LandscapeRight)
            {
                //Update UI
            }

        }, (IUIViewControllerTransitionCoordinatorContext) => {
            //Transition Completion
        });
    }
Kevin Li
  • 2,258
  • 1
  • 9
  • 18
  • Thank you @Kevin, it works for me! Could you, please, help me with other issues: 1. https://stackoverflow.com/questions/46224467/mvvmcross-hamburger-menu-for-ios 2. https://stackoverflow.com/questions/46202789/mvvmcross-platform-exceptions-mvxexception-could-not-load-plugin-assembly-for-t Thank you. – DaleYY Sep 15 '17 at 11:05