Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling two UITableViews together

I have a weird design case in which to implement I need to place two UITableViews on the same view. I would just use sections, but I need an index on the second table.

I'd like them to both scroll together like they were actually two sections of the same table view. Is this possible?

Here's a basic mockup illustrating why:

mockup

As far as I understand it, you cannot move the index. Also, when you add an index to a table, it appears over the entire tableview, not just one section.

If I have to implement this as two table views, they both need to scroll as if they were one. If it's possible to do this using sections, that's even better.

like image 492
James Avatar asked Nov 30 '11 23:11

James


2 Answers

Update

After seeing your update my old answer is not very good but I'll leave my previous answer for anyone else who wants it.

New answer - It depends how you want the tables sync'd.

If you know

  1. Which cells are in the top 5
  2. How tall each cell is
  3. The offset the table view

Then you can calculate which cell is visible at the top of the top 5 table. You can use this information to scroll the bottom table to the correct index.


Old answer

I'm with the other guys not sure why you would want to or if I am misinterpreting but it's pretty simple.

UITableView is a subclass of UIScrollView so you can set yourself as the UITableViewDelegate and override

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
    UITableView *slaveTable = nil;

    if (self.table1 == scrollView) {
        slaveTable = self.table2;
    } else if (self.table2 == scrollView) {
        slaveTable = self.table1;
    }

    [slaveTable setContentOffset:scrollView.contentOffset];
}

This gives me something that looks like this:

enter image description here

The top UITableView is self.table1 and the bottom is self.table2 if either table is scrolled the scrolling is mirrored in the other table.

like image 125
Paul.s Avatar answered Oct 17 '22 09:10

Paul.s


in swift this code:

func scrollViewDidScroll(scrollView: UIScrollView) {
    if tb_time1 == scrollView {
        tb_time2.contentOffset = tb_time1.contentOffset
    }else if tb_time2 == scrollView {
        tb_time1.contentOffset = tb_time2.contentOffset
    }
}
like image 27
Pablo Ruan Avatar answered Oct 17 '22 09:10

Pablo Ruan