简体   繁体   English

使用UIViewController合并静态表视图

[英]Merging static table view with a UIViewController

I'm currently trying to build a screen that contains 2 UIButton , and 1 UIImageView . 我目前正在尝试构建一个包含2个UIButton和1个UIImageView的屏幕。

Below these UI Elements, I want to add a static table view that would contain 1 UITextField for each cell in order to create a kind of scrollable form. 在这些UI元素下面,我想添加一个静态表视图,该表将为每个单元格包含1个UITextField ,以便创建一种可滚动的表单。

The error I'm having is the following one: 我遇到的错误是以下错误:

Static table views are only valid when embedded in UITableViewController instances 静态表视图仅在嵌入UITableViewController实例时才有效

While it doesn't seem possible to create a static table view without a table view controller, I was wondering if there could be any way to get the same result as my initial idea? 尽管没有表视图控制器似乎无法创建静态表视图,但我想知道是否可以通过任何方法获得与最初的想法相同的结果?

Please note that I'm building my UI using storyboard. 请注意,我正在使用情节提要构建我的UI。

Here's a screenshot of what I was trying to build initially: 这是我最初尝试构建的屏幕截图:

(这是我最初尝试构建的屏幕截图)

EDIT: I finally decided to use a static view controller, and implemented the buttons in a cell and the other textfields in different cells. 编辑:我最终决定使用静态视图控制器,并实现了一个单元格中的按钮以及其他单元格中的其他文本字段。 Thank you all for your help. 谢谢大家的帮助。

You can add the UITableViewController as a childViewController to your bigger UIViewController (parentVC) 您可以将UITableViewController作为childViewController添加到更大的UIViewController (parentVC)

Then manage parentVC's view hierarchy so that you can achieve the 2 UIButton , 1 UIImageView and a table view at the bottom 然后管理parentVC的视图层次结构,以便您可以在底部实现2个UIButton ,1个UIImageView和一个表视图

I think you should manage this adding elements in a UIScrollView , there's no need to use a UITableView . 我认为您应该在UIScrollView管理此添加元素,而无需使用UITableView So you can scroll all the contents when you show the keyboard 因此,当您显示键盘时,您可以滚动所有内容

A static tableview is nothing more than a UITableViewController handling the UITableView 's UITableViewDataSource methods on your behalf. 静态tableview只是一个UITableViewController代表您处理UITableViewUITableViewDataSource方法。

You can simply add a UITableView to your UIViewController , set the UITableView datasource to your UIViewController and implement the methods as appropriate. 您可以简单地将UITableView添加到UIViewController ,将UITableView数据源设置为UIViewController并适当地实现方法。

eg 例如

class MyViewController: UIViewController, UITableViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        tableview.datasource = self
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // create UITableViewCell
    }
}

In Xcode 10.2 you can use Container View to implement the UI you described. 在Xcode 10.2中,您可以使用Container View来实现您描述的UI。 Drag and drop a container view object to the required view controller in your storyboard scene: 将容器视图对象拖放到情节提要场景中所需的视图控制器:

容器视图

具有容器视图对象的视图控制器

Then add UITableViewController instance to your storyboard scene: 然后将UITableViewController实例添加到故事板场景中:

UITableViewController

Set Static Cells for it's Content : 为其Content设置Static Cells

静态细胞

Then right-click on Content View that you added in one of the previous steps, and setup it as described on the following screenshots: 然后右键单击在先前步骤之一中添加的Content View ,并按照以下屏幕截图中的说明进行设置:

设置容器视图1 设置容器视图2 设置容器视图3

Setup constrains and cells content. 安装程序会约束和单元格内容。 Then you will see something like that on your testing device: 然后,您将在测试设备上看到类似的内容:

在设备上运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM