【问题标题】:Scrollview with embedded tableview带有嵌入式表格视图的滚动视图
【发布时间】:2015-09-09 22:01:32
【问题描述】:

我正在使用 Xcode 6 快速构建一个 iOS 应用程序。 我正在尝试在滚动视图中嵌入带有表格视图的视图控制器。当用户在表格视图中拖动时,假设移动表格,而不是它嵌入的滚动视图。 我做了这个插图,以明确我的视图和视图控制器层次结构:

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

绿色和蓝色区域是不同的视图控制器,嵌入在滚动视图中。

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

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

我在滚动视图中启用了分页,以便它捕捉到绿色或蓝色视图控制器。如何将表格视图弹出到视图层次结构的顶部,以便滚动滚动视图的唯一方法是在文本字段中拖动。

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的高度吗?
  • 如果tableView的内容大于tableView的可视区域,那么tableView是否应该滚动?

标签: ios swift view uiscrollview subview


【解决方案1】:

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

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

使用的每个不同的单元格都将被视为一个部分,原型单元格的数量将等于代码中的部分数量,如下面的 sn-p

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?

【讨论】:

    【解决方案2】:

    我对此进行了嘲笑。我的视图层次结构如下所示

    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 不会)。

    这就是你想要达到的目标吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 2013-12-30
      相关资源
      最近更新 更多