0

I currently have a UITableViewController, but would like to add a label at the bottom of the screen. I think the easiest way to do this would be to just use a UIViewController with a UITableView. Is there an easy way to migrate over? I've noticed some functionally is different; for example UITableViewController has a numberOfSections method, whereas UITableView does not have that method. Also is it possible to use a UIViewController as the delegate for a UITableView? Thanks in advance.

Update: I have attached a picture of the code, and the error given when trying to implement UITableViewDataSource.

Source Code

Matt P
  • 55
  • 10

3 Answers3

0

As far as I know, the methods are the same, you just won't need the override bit before each one if its just a UITableView. You can use UIViewController as the delegate, yes, by setting myTableView.delegate = self in its viewDidLoad method.

If its just a label you need however, you might consider adding one programatically? Similar results achieved with a no data label for example - see here. You could then play around with the positioning.

Tom
  • 513
  • 2
  • 5
  • 20
  • Hi, I cannot adopt the UITableViewDelegate as a UIViewController, so the methods won't transfer over. – Matt P Aug 08 '17 at 03:10
  • Have you added the UITableViewDataSource and UITableViewDelegate protocols to the class declaration? Perhaps you could update the original question with your code? – Tom Aug 08 '17 at 09:21
  • Hi Tom, please see the updated question, which now has a picture of the source code. – Matt P Aug 08 '17 at 15:07
0

You can do this. You need to implement the UITableViewDelegate and include the UITableViewDatasource in your View Controller, then you can use the functions you were using in your TableViewController.

Basically what you should do is add a tableview in the interface builder, or programmatically (depending on your choice). If it's done through the interface builder then outlet it to your Controller. Add the Delegate to your class, and you would have access to the tableViewFunctions (cellForRow, numberOfItems, numberOfSections, didSelectRow etc...).

Also, when you create the tableview specify the tableview.delegate = self and tableview.datasource = self.

Alan
  • 1,132
  • 7
  • 15
0

This error is thrown because you need to set dataSource to your TableView Object like self.tableView.dataSource = self. After this you can use freely all methods for UITableView including numberOfSections as you mentioned.

Stefan
  • 1,283
  • 15
  • 33