How to link Table dataSource and delegate outlets to UITableViewController?

╄→гoц情女王★ 提交于 2019-12-06 00:36:44

问题


I'm new to Swift/iOS development.

I have a UITableView in my storyboard that I want to populate with some data. In my attempt to do so, I created a class that inherits from UITableViewController. The implementation is not yet complete, but my understanding is that by inheriting from this class I can provide an IBOutlet to both dataSource and delegate.

When I try to drag the outlet into the source file, I don't get the insertion point that I got when I tested this before. See image below:

What do I have to do to define this class as the handler for the UITableView?


回答1:


In your storyboard, select the UITableView and change this to the name of your UITableViewController subclass. You then do not need to do any linking stuff.




回答2:


Set your viewController to inherit from UIViewController, not from UITableViewController (It seems like your IB is set up like that). Do not forget to set your class to ZeroconfTableController on the interface builder.

Than, you will be able to set the delegate and datasource. NOTE: the delegate and the datasource will not create IBOutlets.

Assign the delegate and the dataSource the following way:

Also make sure, your viewController conforms to the protocols.

class ZeroconfTableController: UIViewController, UITableViewDataSource, UITableViewDelegate { 



回答3:


If you're using your own UITableView, inherit from UIViewController, not UITableViewController.

If however, you want to use a UITableViewController (which personally I don't use) in your storyboard, then do inherit from UITableViewController. In this case you won't have to wire up the UITableViewDataSource nor the UITableViewDelegate.



来源:https://stackoverflow.com/questions/40346078/how-to-link-table-datasource-and-delegate-outlets-to-uitableviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!