Ios 带有嵌入式tableview的Scrollview
我正在用Xcode 6在swift中构建一个iOS应用程序。 我正在尝试在scrollview中嵌入带有表视图的视图控制器。当用户拖动表视图时,假定它移动表,而不是它嵌入的滚动视图。 我制作了此插图,以明确我的视图和视图控制器层次结构: 红色区域是scrollview的内容大小区域 绿色和蓝色区域是嵌入在滚动视图中的不同视图控制器 黄色区域是蓝色视图控制器中的文本字段 橙色区域是蓝色视图控制器中的表视图 我在scrollview中启用了分页功能,因此它可以捕捉到绿色或蓝色视图控制器。如何将表格视图弹出到视图层次结构的顶部,以便滚动scrollview的唯一方法是在文本字段中拖动Ios 带有嵌入式tableview的Scrollview,ios,swift,view,uiscrollview,subview,Ios,Swift,View,Uiscrollview,Subview,我正在用Xcode 6在swift中构建一个iOS应用程序。 我正在尝试在scrollview中嵌入带有表视图的视图控制器。当用户拖动表视图时,假定它移动表,而不是它嵌入的滚动视图。 我制作了此插图,以明确我的视图和视图控制器层次结构: 红色区域是scrollview的内容大小区域 绿色和蓝色区域是嵌入在滚动视图中的不同视图控制器 黄色区域是蓝色视图控制器中的文本字段 橙色区域是蓝色视图控制器中的表视图 我在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)
}
我希望我已经清楚地表达了我自己
编辑:
我尝试过在滚动时更改scrollview的大小。这样做的目的是改变框架的高度,使其在向下滚动时与文本字段的高度相匹配。但它似乎也改变了scrollview中嵌入的可见部分:
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)
}
}
我模拟了这个。我的视图层次结构如下所示
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
连接到可滚动区域的uicrollview
(滚动视图)和UIView
(containerView)
在viewDidLoad
中,我添加了:
scrollView.contentSize = containerView.frame.size
如果单击tableView之外的任何位置(顶部区域、文本区域等),我将滚动scrollView。如果我尝试在表格视图中滚动,表格视图会滚动(滚动视图不会)
这就是你想要达到的目标吗?好吧,现在可能有点晚了,但我还是将此作为教程发布! 这是一种不太受欢迎的实现方法。更好的办法是 使用表视图控制器作为父视图,然后使用原型单元作为静态单元(根据您的要求使用“n”个数字)作为卡视图或任何其他用途 所使用的每个不同的单元都将被视为一个节,原型单元的数量将等于代码中的节的数量,如下面的代码片段所示
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节,即动态部分,行数将等于列表计数
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;
}
就这样!工作完成了!派对
我从这里得到了推荐信
?tableView是否设置了其委托?是否确定tableView中的内容超过了tableView的高度?如果tableView的内容大于tableView的可视区域,那么tableView是否应该滚动?