Boris88
Boris88

Reputation: 297

How to implement side-by-side tableviews

I'm working on an app in which there is a social feed. I would really like to create a custom view with two tableViews side-by-side. I want to know how you would code this. Is it possible to intercept the scrolling of a table to move the other one at the exact time? Or maybe there is an easier way to achieve this?

I always say the is no limit in programming but the programmer limits. Here's mine.

Thank you for your ideas!

This what I want to do : enter image description here

Upvotes: 0

Views: 250

Answers (2)

staticVoidMan
staticVoidMan

Reputation: 20274

Assuming:

  1. Your UITableView objects are called:
    • tableView1
    • tableView2
  2. You've hooked their delegates properly
    • [tableView1 setDelegate:self];
    • [tableView2 setDelegate:self];

Pre-Requisite:

  1. Declare NSInteger i_check; globally

Implement these scrollView delegate methods:

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (scrollView == tableView1) {
        i_check = 1;
    }
    else if (scrollView == tableView2) {
        i_check = 2;
    }
    else { // just incase you have a scrollView that you don't want to track
        i_check = 0;
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (i_check == 1) {
        [tableView2 setContentOffset:scrollView.contentOffset];
    }
    else if (i_check == 2) {
        [tableView1 setContentOffset:scrollView.contentOffset];
    }
}

A UITableView internally uses a UIScrollView and since the UITableViewDelegate publicly declares UIScrollViewDelegate, you can access all the scrollView delegate methods by simply setting the tableView object delegate (which you will be doing anyways)

Upvotes: 2

Taum
Taum

Reputation: 2551

It could be implemented with a UICollectionView.

This should help you a good deal: https://www.cocoacontrols.com/controls/waterfallcollectionview

Upvotes: 1

Related Questions