带嵌套表格的滚动视图

17
我正在使用Xcode 6用Swift构建iOS应用程序。我试图将带有表视图的视图控制器嵌入滚动视图中。当用户在表视图中拖动时,它应该移动表格,而不是嵌入其中的滚动视图。
这张图片清晰地展示了我的视图和视图控制器层次结构:View Hierachy 红色区域是滚动视图的内容大小区域。
绿色和蓝色区域是不同的视图控制器,嵌入在滚动视图中。
黄色区域是蓝色视图控制器中的文本字段。
橙色区域是蓝色视图控制器中的表视图。
我已经在滚动视图中启用了分页功能,这样它就可以自动定位到相应的绿色或蓝色视图控制器。那么我如何将表视图放置在视图层次结构的顶部,以便只能通过在文本字段中拖动来滚动滚动视图呢?
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)

}

我希望我已经表达清晰。

编辑:

我尝试更改滚动视图的大小,使其在滚动时自适应文本框的高度,但似乎这也会改变滚动视图中嵌入内容的可见部分:

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

tableView 是否设置了它的代理?你确定 tableView 中的内容超过了 tableView 的高度吗? - djv
如果tableView的内容大于tableView的可视区域,那么tableView应该滚动吗? - DJohnson
2个回答

4

好的,可能有点晚了,但我仍然将其发布为教程! 这是一种较不理想的方法来实现此目标。更好的方法是:

使用表视图控制器作为父视图,然后使用原型单元格作为静态单元格(在“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;

就是这样了!工作完成了!派对时间到了!

我从这里得到了参考:在分组表视图中混合静态和动态部分


0

我做了一个模拟。我的视图层次结构如下

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中滚动,tableView会滚动(而不是scrollView)。

这就是你想要实现的吗?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接