ios - 带有嵌入式表格 View 的 ScrollView

标签 ios swift view uiscrollview subview

我正在使用 Xcode 6 快速构建一个 iOS 应用程序。 我正在尝试在 ScrollView 中嵌入一个带有 TableView 的 View Controller 。当用户在 TableView 中拖动时,应该移动表,而不是它嵌入的 ScrollView 。 我做了这个插图,以清除我的 View 和 View Controller 层次结构: View Hierachy

红色区域是 ScrollView 的内容大小区域

绿色和蓝色区域是不同的 View Controller ,嵌入在 ScrollView 中。

黄色区域是蓝色 View Controller 中的文本字段。

橙色区域是蓝色 View Controller 中的 TableView 。

我在 ScrollView 中启用了分页,因此它会捕捉到绿色或蓝色 View Controller 。如何将 TableView 弹出到 View 层次结构的顶部,以便滚动 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)
    }
}

最佳答案

好的,现在可能有点晚了,但我仍然将其作为教程发布! 这是一种不太受欢迎的实现方式。更好的方法是,

使用 TableView Controller 作为父 View ,然后使用原型(prototype)单元格作为静态单元格(根据您的要求在'n'个数字中)作为卡片 View 或用于任何其他用途

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

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;

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

我从这里得到引用 Mixing static and dynamic sections in a grouped table view

关于ios - 带有嵌入式表格 View 的 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32489830/

相关文章:

ios - swift 。在 ViewController 之间转换而不在返回时关闭它们

ios - 我的 xib 文件中的自动布局约束有什么问题?

sql-server - 为什么索引 View 不能有 MAX() 聚合?

view - mysqldump 输出中 View 定义之前的 DROP TABLE 不正确

ruby-on-rails - ruby rails : re-order checkbox tag

ios - 为什么 IOS 应用程序在使用 Google Places API 时崩溃?

ios - 单击取消按钮不返回我的iOS应用

swift - removeFromParentNode 不释放 ARKit 应用程序中的内存

ios - SKShapeNode - 动画颜色变化

ios - 在快速输入期间使用搜索栏进行搜索时 TableView 崩溃