IPhone - how to scroll two UITableViews symmetrically

In my application, I have two tableViews side by side. When the user scrolls, I would like the second to scroll at the same time, so it looks almost like one table with two different columns. I lost a little, how to do this, any suggestions?

Thanks Greg

+4
source share
4 answers

You need to look at UIScrollViewDelegate - let's say you have two types of scrolling, A and B.

Use the delegate method scrollViewDidScroll to scroll A to get the offset, and then call setContentOffset in scroll mode B in the same method, passing in the value you get from the delegate.

In fact, this should not exceed 2-3 lines of code after setting delegate methods.

+8
source

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

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView 

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

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

and set a 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 use it in a UITableView if you want, but there is no particular reason for this.

+10
source

also, viewing the table that was scrolled by the user should not be sent to setContentOffset: message in scrollViewDidScroll, since it will receive the application in an infinite loop. therefore, to solve the problem, it is necessary to implement additional UIScrollViewDelegate methods:

 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { beingScrolled_ = nil; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { if(beingScrolled_ == nil) beingScrolled_ = scrollView; } 

and changing the version of Inspire48 scrollViewDidScroll: respectively:

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

where beingScrolled_ is an ivar of type UIScrollView

+7
source

in swift scroll two symmetric images symmetrically:

 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 } } 
0
source

All Articles