iPhone - 如何对称滚动两个 UITableViews

发布于 2024-11-28 03:35:54 字数 112 浏览 0 评论 0原文

在我的应用程序中,我有两个并排的 tableView。当用户滚动时,我希望第二个同时滚动,因此它看起来几乎就像一个具有两个不同列的表。我对如何去做这件事有点迷失,有什么建议吗?

谢谢, 格雷格

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

攒一口袋星星 2024-12-05 03:35:54

方便的是,UITableView 是 UIScrollView 的子类。存在一个 UIScrollViewDelegate,它具有以下方法:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

如果实现该方法,则可以获得 scrollView 参数的 contentOffset 属性。然后,您应该使用

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

并设置新的内容偏移量。所以像这样:

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

如果您愿意,您可以转换为 UITableView,但没有特别的理由这样做。

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.

别低头,皇冠会掉 2024-12-05 03:35:54

您需要查看 UIScrollViewDelegate - 假设您有两个滚动视图,AB

使用滚动视图AscrollViewDidScroll委托方法获取偏移量,然后在同一方法中调用滚动视图B上的setContentOffset ,传入从委托处获得的值。

一旦您设置了委托方法,实际上不应超过 2-3 行代码。

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.

青衫儰鉨ミ守葔 2024-12-05 03:35:54

另外,用户滚动的 tableview 不应该在scrollViewDidScroll 中发送 setContentOffset: 消息,因为它会使应用程序陷入无限循环。因此应该实现额外的 UIScrollViewDelegate 方法来解决问题:

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

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

并相应地修改 Inspire48 的版本 rollViewDidScroll:

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

其中beingScrolled_是 UIScrollView 类型的 ivar

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

淡莣 2024-12-05 03:35:54

UITableView 是 UIScrollView 的子类。快速解决方案:

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

并停止两个表格的反弹

tableViewLeft.bounces = false
tableViewRight.bounces = false

如果你想让垂直滚动指示器消失,请编写以下代码

tableViewLeft.showsVerticalScrollIndicator = false
tableViewRight.showsVerticalScrollIndicator = false

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
仅此而已 2024-12-05 03:35:54

快速滚动两个 uitableviews 对称:

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

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文