Embedding a TableView in a TableViewController in a different view

I would like to customize the contents of the table view controller (add a few buttons, etc.), but the root view for it is the table view. Is this a way to embed it in another view and add controls to it?

+6
source share
2 answers

You can use Container View .

In the storyboard, create another view controller that you will use to place these controls. Then drag and drop the Container view from the object library (usually the last item in the list) and place it where you want the table view to appear (it could be the whole screen). To do this, Container View will create another view controller. Just remove this and ctrl drag from View container to the table view controller and select Paste segue.

Thus, you do not need to change anything in your current table view controller if it is independent of other view controllers in order to provide it with data to make it work. However, if that is the case, you can assign a segue identifier to embed segue, implement prepareForSegue:sender: and do whatever you usually do with other segments.

+22
source

You simply add a TableView as a sub-element of the uiviewcontroller. Remember to call the delegates. I usually do not use storyboards, but if you do this programmatically, you can do it like this:

In the .h file:

 @interface EmbeddedTableView : UIViewController <UITableViewDelegate, UITableViewDataSource> 

And on the .m file, just create your tableView by adding it as a subview of the viewcontroller.

You can look at this, for example: How to configure a UITableView in a UIViewController created in a .xib file

+1
source

Source: https://habr.com/ru/post/978392/


All Articles