How to setup a static UITableView as a subview of UIView?
Solution 1
Here is what you can do. In your storyboard, create a parent view controller that contains all your non tableview views also, create a UITableViewController. In your parent view controller, create container view, delete the view controller it auto adds, and right click and drag from the container view to your UITableViewController to create an embed segue. Your end result should look something like this:
Solution 2
You still need to do a couple of things:
Add <UITableViewDataSource, UITableViewDelegate>
to your @interface declaration.
Then you can set these also in Interface Builder.
Implement cellForRowAtIndexPath
and call the dequeueReusableCellWithIdentifier
method to return the cell.
Sorry, I was wrong. The truth is you cannot use static cells without a UITableViewController
. Sorry.
A solution could be that you create two controllers and just add the view
of the table view controller to your other view controller.
Solution 3
As far as I know, you can't do this directly. At least in iOS 6, you had to use a UITableViewController when using static cells. One way to use a static table view inside a UIViewController would be to add a container view in IB, and make the embedded controller a table view controller (delete the UIViewController you get automatically, drag in a UITableViewController, and hook it up with the embed segue). You can get a reference to this table view controller from the UIViewController by implementing prepareForSegue:sender:, and using the destinationViewController property (which will point to the table view controller).
vzm
Updated on June 12, 2022Comments
-
vzm almost 2 years
When I work with a
TableViewController
I am able to setup all my content in storyboards. Since I use Static Cells instead of Dynamic Properties for my table view, I find this method much more convenient and easier to implement. I hook up the new UITableView class and simply delete all the delegate methods. Works like a charm as ALL of the content / buttons are being setup in storyboards.I am trying to accomplish the same result, except this time, I have to work within a
ViewController
and add aTableView
as a subview. Once I hook up the right class, add my outlet connection and setup the following delegates:- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 3; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"MainCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; return cell; }
This works well if my
TableView
is set to Dynamic Properties :But when I change the Table View content to Static Cells and delete the delegate method, my app crashes. So, How can I add a table view with Static Cells (That I can manipulate in storyboards) to my
ViewController
? -
rdelmar over 10 yearsUnless something has changed in iOS 7, you're not supposed to implement the data source methods when using static cells.
-
vzm over 10 years@rdelmar that is exactly right. I have never done so, and this works just fine when working with a TableViewController, its not working for me when I try to add a table view to my view controller.
-
vzm over 10 years@Mundi, although I have already added
<UITableViewDataSource, UITableViewDelegate>
to my @interface, I don't think that is the problem, see comment above from rdelmar, which confirms my suspicion. -
vzm over 10 years@Mundi, in response to your updated answer, how can I "add the view of the table view controller to your other view controller."?
-
Mundi over 10 years
[self.view addSubview:tableViewController.view];
-
Steven about 10 yearsI have something like this setup, but how do I communicate with each other? the parent and child view.
-
JonahGabriel about 10 years@Steven In the parent view controller, you can add a property of the child view controller and set it in prepareForSegue.
-
Steven about 10 yearsYea, after the comment. I figured to set things in the
prepareForSegue
. Thanks tho. -
haris over 8 yearsWorks like a charm. Thank you very much.
-
JonahGabriel over 8 years@n00b Glad I could help :)