Keep two UITableViews in "scroll sync" ?

JeffRushJeffRush ✭✭USMember ✭✭

I have two UITableViews. They contain basically the same rows, but each one has different information. I detect left and right swipes and then animate the views so that one appears to "slide off" the screen while the other "slides on". They can go in either direction.

There could be up to 20 rows in them. If they scroll in one, I'd like the other one to scroll with it so that when they swipe left or right, they stay aligned. Hopefully that makes sense.

Is there an event that will detect when the UITableView has been scrolled so that I could then "force" the other UITableView to scroll to the same exact spot?

Posts

  • DannyCDannyC ✭✭✭✭ US ✭✭✭✭
    edited November 2013

    @JeffRush UITableView does have a "Scrolled" event handler and then you can use the "ScrollToRow" method to force your second UITableView to scroll to the same row. Hope this helps.

  • JeffRushJeffRush ✭✭ USMember ✭✭

    Thanks Danny. I actually tried that before my post and it worked as long as the user had scrolled exactly so that the top of a row aligned with the top of the table. In other words, if at the top of the first UITableView you could only see half of the top row, the second UITableView would not quite be exactly aligned.

  • LarryOBrienLarryOBrien Xamurai USXamarin Team Xamurai

    Have you tried to synchronize on the ContentOffset property? UITableView is a subclass of UIScrollView, so if the tables have the same dimensions, my hunch is that would be worth exploring.

  • JeffRushJeffRush ✭✭ USMember ✭✭

    I ended up using ScrollToRow on the first table as suggested so that it will always show the entire top row before I keep the other table in sync.

  • kunsanipkunsanip Member

    using Foundation;
    using System;
    using UIKit;
    using CoreGraphics;
    using System.Collections.Generic;

    namespace ScrollTesting
    {
    public partial class ViewController : UIViewController,IUITableViewDelegate, IUITableViewDataSource
    {

        public ViewController(IntPtr handle) : base(handle)
        {
        }
    
        UIScrollView scrollView;
        UITableView tableView1;
        UITableView tableView2;
        String CellIdentifier = "Scroll";
        private int[] listNumber = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 };
    
        nfloat screenHeight = UIScreen.MainScreen.Bounds.Height;
    
        nfloat scrollViewContentHeight = 1200;
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
    
            nfloat h = 50.0f;
            nfloat w = 50.0f;
            nfloat padding = 10.0f;
            nint n = 100;
    
            scrollView = new UIScrollView
            {
                Frame = new CGRect(0, 0, View.Bounds.Width, View.Bounds.Height),
                //ContentSize = new CGSize(View.Bounds.Width, View.Bounds.Height+h),
                BackgroundColor = UIColor.LightGray,
                AutoresizingMask = UIViewAutoresizing.FlexibleWidth
            };
            tableView1 = new UITableView
            {
                Frame = new CGRect(0, 0, View.Frame.Width/2, View.Frame.Height),
                Delegate = this,
                DataSource = this
            };
            tableView2 = new UITableView
            {
                Frame = new CGRect(View.Frame.Width / 2, 0, View.Frame.Width / 2, View.Frame.Height),
                Delegate = this,
                DataSource = this
            };
    
            scrollView.AddSubview(tableView1); 
            scrollView.AddSubview(tableView2);
    
            View.AddSubview(scrollView);
    
        }
    
        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        }
    
        public nint RowsInSection(UITableView tableView, nint section)
        {
            return listNumber.Length-1;
        }
    
        public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            var cell = tableView.DequeueReusableCell(CellIdentifier);
            if (cell == null)
            { cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier); }
    
            cell.TextLabel.Text = listNumber[indexPath.Row].ToString();
    
            return cell;
        }
    }
    

    }

    This works perfect!! Enjoy!!

  • kunsanipkunsanip Member

    //ContentSize = new CGSize(View.Bounds.Width, View.Bounds.Height+h),

    Don't forget to uncomment this line. :smile:

Sign In or Register to comment.