Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - how to scroll two UITableViews symmetrically

In my app, I have two tableViews side by side. When the user scrolls on, I would like the second one to scroll at the same time, so it looks almost like one table with two different columns. I'm a bit lost on how to go about doing this, any suggestions?

Thanks, Greg

like image 969
Greg Avatar asked Nov 27 '22 14:11

Greg


1 Answers

Conveniently, UITableView is a subclass of UIScrollView. There exists a UIScrollViewDelegate, which has this method:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

If you implement that method, you can get the contentOffset property of the scrollView argument. Then, you should use

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

and set the new content offset. So something like this:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    UIScrollView *otherScrollView = (scrollView == self.tableView1) ? self.tableView2 : self.tableView1;
    [otherScrollView setContentOffset:[scrollView contentOffset] animated:NO];
}

You can cast to a UITableView if you'd like, but there's no particular reason to do so.

like image 81
FeifanZ Avatar answered Dec 05 '22 00:12

FeifanZ