简体   繁体   中英

Merging static table view with a UIViewController

I'm currently trying to build a screen that contains 2 UIButton , and 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.

The error I'm having is the following one:

Static table views are only valid when embedded in UITableViewController instances

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.

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)

Then manage parentVC's view hierarchy so that you can achieve the 2 UIButton , 1 UIImageView and a table view at the bottom

I think you should manage this adding elements in a UIScrollView , there's no need to use a 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.

You can simply add a UITableView to your UIViewController , set the UITableView datasource to your UIViewController and implement the methods as appropriate.

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. 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

Set Static Cells for it's Content :

静态细胞

Then right-click on Content View that you added in one of the previous steps, and setup it as described on the following screenshots:

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

Setup constrains and cells content. Then you will see something like that on your testing device:

在设备上运行

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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