iPhone - how to scroll two UITableViews symmetrica

2019-02-10 21:19发布

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

5条回答
Anthone
2楼-- · 2019-02-10 21:38

UITableView is a subclass of UIScrollView. Swift solution:

extension yourViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let otherScrollView = (scrollView == tableViewLeft) ? tableViewRight : tableViewLeft
        otherScrollView?.setContentOffset(scrollView.contentOffset, animated: false)
    }
}

And stop the bounce of both the tables

tableViewLeft.bounces = false
tableViewRight.bounces = false

If you want to make vertical scroll Indicators go, write the below code

tableViewLeft.showsVerticalScrollIndicator = false
tableViewRight.showsVerticalScrollIndicator = false
查看更多
ゆ 、 Hurt°
3楼-- · 2019-02-10 21:53

in swift scroll two uitableviews 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
    }
}
查看更多
地球回转人心会变
4楼-- · 2019-02-10 21:58

also, the tableview that got scrolled by the user should not be sent setContentOffset: message in scrollViewDidScroll, since it will get the app into endless cycle. so additional UIScrollViewDelegate methods should be implemented in order to solve the problem:

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

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

and modifying Inspire48's version scrollViewDidScroll: accordingly:

- (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

查看更多
劫难
5楼-- · 2019-02-10 22:00

You'll want to look into the UIScrollViewDelegate - say you've got two scroll views, A and B.

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

It actually shouldn't be more than 2-3 lines of code once you've set-up your delegate methods.

查看更多
太酷不给撩
6楼-- · 2019-02-10 22:00

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.

查看更多
登录 后发表回答