首页 > 解决方案 > 两个并排的 UITableView 同时滚动

问题描述

我有两个UITableView并排在一起UIViewController。我希望这些 tableViews 同时滚动。这里我用过contentOffset。但问题是,当我尝试通过触摸右 tableView 滚动表格视图时,它正在工作。但是,当我尝试通过触摸左侧 tableView 来滚动表格视图时,它不起作用。如何通过触摸任何 tableView 一次滚动它们?

这是我的代码 -

func scrollViewDidScroll(_ scrollView: UIScrollView) {

    tableViewTwo.contentSize.height = tableViewOne.contentSize.height
    tableViewOne.contentOffset = tableViewTwo.contentOffset

}

标签: swiftuiscrollviewtableview

解决方案


您可以尝试以下方法:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView == tableView1 {
        tableView2.contentOffset = scrollView.contentOffset
        tableView2.contentSize.height = scrollView.contentSize.height
    } else {
        tableView1.contentOffset = scrollView.contentOffset
        tableView1.contentSize.height = scrollView.contentSize.height
    }
}

推荐阅读