滚动查看与embedded式tableview

我正在用Xcode 6快速构build一个iOS应用程序。我试图在滚动视图中embedded一个表视图的视图控制器。 当用户在表格视图中拖动时,假设是移动表格,而不是它embedded的滚动视图。我已经做了这个例子,以清除我的视图和视图控制器层次结构: 查看Hierachy

红色区域是滚动视图内容大小区域

绿色和蓝色区域是embedded在滚动视图中的不同视图控制器

黄色区域是蓝色视图控制器中的文本字段。

橙色区域是蓝色视图控制器中的表格视图。

我已经在滚动视图中启用分页,以便它捕捉到绿色或蓝色的视图控制器。 如何将Table视图popup到视图的顶层,这样滚动scrollview的唯一方法就是在文本框中拖动。

import UIKit class RootViewController: UIViewController, UIScrollViewDelegate { var scrollView: UIScrollView! var greenViewController: GreenViewController! var blueViewController: BlueViewController! override func viewDidLoad() { super.viewDidLoad() scrollView = UIScrollView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)) scrollView.delegate = self scrollView.pagingEnabled = true self.greenViewController = self.storyboard?.instantiateViewControllerWithIdentifier("Green View Controller") as! GreenViewController self.blueViewController = self.storyboard?.instantiateViewControllerWithIdentifier("Blue View Controller") as! BlueViewController greenViewController.view.frame = CGRectMake(0, 0, view.bounds.width, view.bounds.height) blueViewController = CGRectMake(0, view.bounds.height, view.bounds.width, view.bounds.height) scrollView.addSubview(greenViewController.view) scrollView.addSubview(blueViewController.view) scrollView.contentSize = CGSizeMake(view.bounds.width, view.bounds.height*2) self.view.addSubview(scrollView) } 

我希望我已经expression清楚。

编辑:

我试过改变scrollview的大小,当它滚动。 这个想法是改变框架的高度,以便当它一直向下滚动时,它与文本框的高度相匹配。 但它似乎也改变了scrollview中embedded内容的可见部分:

  func scrollViewDidScroll(scrollView: UIScrollView) { if self.scrollView.contentOffset.y > textField.View.bounds.height { self.scrollView.frame.size.height = view.bounds.height - scrollView.contentOffset.y - textField.View.bounds.height println(self.scrollView.frame) } } 

好吧,现在可能有点晚了,但仍然im作为教程张贴这个! 这是一个不太可取的方法来实现这一点。 更好的方法是,

使用表视图控制器作为父视图,然后使用原型单元格作为静态单元格(按您的要求inputn个数字)作为卡片视图或用于任何其他用途

所使用的每个不同的单元格都将被视为一个部分,原型单元格的数量将不会等于代码中的部分数量,如下面的代码片段所示

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 3 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if section == 2 { return list.count } return 1 } 

在0和1的情况下的行数将是1作为静态部分而在部分2的情况下的行数也就是dynamic部分将等于列表的数量。

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell : CustomTableViewCell.swift = CustomTableViewCell.swift() switch indexPath.section { case 0: cell = tableView.dequeueReusableCellWithIdentifier("staticCell1", forIndexPath: indexPath) as! CustomTableViewCell break case 1: cell = tableView.dequeueReusableCellWithIdentifier("staticCell2", forIndexPath: indexPath) as! CustomTableViewCell break case 2: cell = tableView.dequeueReusableCellWithIdentifier("dynamicCell", forIndexPath: indexPath) as! CustomTableViewCell break default: break } return cell; 

}

这就是它! 工作已经完成! 派对!

我从这里获得参考在分组表视图中混合静态和dynamic部分 ?

我嘲笑了这一点。 我的视图层次结构如下所示

 ViewController's UIView ...UIView (to act as a container view for all the scrollable content) .......UIView (for the top content) - green .......UIView (for the bottom content) - blue ............UILabel ............UITableView (with scrollable content - more rows than visible) 

我将@IBOutlets连接到了可滚动区域的UIScrollView (scrollView)和UIView (containerView)。

viewDidLoad我添加了:

 scrollView.contentSize = containerView.frame.size 

如果我点击tableView(顶部区域,文本区域等)之外的任何地方,我滚动scrollView。 如果我尝试在表视图中滚动,tableView滚动(scrollView不)。

那是你想要达到的目标吗?