【问题标题】:How to scroll table view with the whole screen如何在整个屏幕上滚动表格视图
【发布时间】:2019-05-21 15:59:47
【问题描述】:

我想在我的视图控制器中滚动并显示整个 table view。

像这样,我希望collection view在滚动时向上移动,table view的内容将显示在整个屏幕上。

但我现在得到的是表格视图在表格视图部分内滚动,而集合视图没有向上移动。

这是我的视图控制器的层次结构,我在 Scroll View 中有 Collection View 和 Table View

我检查了this post 和this post,但没有工作。

【问题讨论】:

    标签: swift xcode uitableview tableview


    【解决方案1】:

    您可以在tableView's headerView中添加collectionView,而不是在controller'sview中单独添加collectionView。

    1.创建一个自定义UIView - HeaderView,其中将包含一个UICollectionView

    class HeaderView: UIView {
        @IBOutlet weak var collectionView: UICollectionView!
    
        //Add collectionView dataSource and delegate methods here
    }
    

    2. 将上面创建的HeaderView 添加为tableView's headerView 在您的controller 中。

    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
        @IBOutlet weak var tableView: UITableView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            //Here.....
            if let headerView = Bundle.main.loadNibNamed("HeaderView", owner: self, options: nil)?.first as? HeaderView {
                self.tableView.tableHeaderView = headerView
            }
        }
    
        //add UITableViewDataSource and UITableViewDelegate methods here...
    }
    

    在故事板中 - 在 controller 中添加 tableView 并将其 - top, bottom, left and right constraints 固定到 controller's view。

    输出:

    【讨论】:

    • 嗨,但是这样我不能将集合视图单元格添加到标题视图 xib 并将 segue 添加到 didSelectItemAt 函数?
    • 在单独的 xib 中创建 collectionViewCell,而不是使用 segue,您可以以编程方式导航。
    • 对不起,我没有说清楚。我想点击不同的collectionViewCell 并推送到不同的ViewController。但是我不能在xib文件中使用self.navigationController?.pushViewController(desVC, animated: true)。
    【解决方案2】:

    您可以尝试将集合视图放在一个单元格中,并将该单元格作为表格视图中的第一行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      相关资源
      最近更新 更多