带有嵌入式tableview的Scrollview [英] Scrollview with embedded tableview

查看:105
本文介绍了带有嵌入式tableview的Scrollview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building an iOS app in swift with Xcode 6. I'm trying to embed a view controller with a table view in a scrollview. When the user drags in the table view, it is suppose to move the table, not the the scrollview that it is embedded in. I've made this illustration, to clearify my view and view controller hierachy:

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

The red area is the content size area of the scrollview.

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

The green and blue areas are different view controllers, embedded in the scrollview.

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

The yellow area is a Text field in the blue view controller.

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

The orange area is a table view in the blue view controller.

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

I have enabled paging in the scrollview, so that it snaps to either the green or blue view controller. How can I pop the Table view to the top of the view hierachy, so that the only way to scroll the scrollview, will be to drag in the text field.

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)

}

我希望我已经表达清楚。

I hope that I have expressed myself clearly.

我在滚动时尝试更改滚动视图的大小。我们的想法是改变框架的高度,使其在完全向下滚动时与文本字段的高度相匹配。但它似乎也改变了嵌入在scrollview中的可见部分:

I've tried changing the size of the scrollview, when it scrolls. The idea was to change the height of the frame so it matches the height of the textfield when it is scrolled all the way down. But it seems that it also changes the visible part of whats embedded in the 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)
    }
}


推荐答案

<好吧现在可能有点迟了但仍然把它作为教程发布!
这是一种不太受欢迎的方法。更好的方法是,

Ok it might be bit late now but still i m posting this as a tutorial ! This is a less prefered way to achieve this. Better way would be,

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

The every different cell used would be considered as a section and no of prototype cells will be equal to no of sections in code as in snippet below

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节的情况下的行数,即动态部分的数量相等计数列表。

number of rows in case of section 0 and 1 would be 1 as static part Whereas No of rows in case of section 2 i.e dynamic part would be equal to count of list.

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;

}

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

and thats it! Work is done! Party!

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

I got reference from here Mixing static and dynamic sections in a grouped table view?

这篇关于带有嵌入式tableview的Scrollview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆