[英]How to create a table view on a view controller when the view controller already inherits from a base view controller
I've got a view controller (TableViewController.swift) which inherits from a base view controller (BaseViewController.swift) like so:- 我有一个视图控制器(TableViewController.swift),它继承自基本视图控制器(BaseViewController.swift),如下所示:
class TableViewController: BaseViewController {
// Some code
}
I want to create a table view on my TableViewController but to do that you have to inherit from UITableViewController but I can't do that because Swift doesn't allow multiple inheritance; 我想在TableViewController上创建一个表视图,但是要做到这一点,您必须从UITableViewController继承,但是我不能这样做,因为Swift不允许多重继承。 is there a way to solve this problem?
有办法解决这个问题吗? I am new to Swift.
我是Swift的新手。
A tableView is just a view underneath, and you can add it in any view controller you desire. tableView只是其下的一个视图 ,您可以将其添加到所需的任何视图控制器中。
A tableViewController on the other hand is a viewController that comes with a built in tableView. 另一方面,tableViewController是带有内置tableView的viewController。
You don't need to subclass tableView only if you want to change its current functionality or appearance. 仅在要更改其当前功能或外观时,才不需要子类化tableView 。
Just add a table view in your view controller and conform to UITableViewDataSource protocol by implementing its required methods ( numberOfSections, numberOfRows and cellForRow ..). 只需在您的视图控制器中添加一个表视图,并通过实现其必需的方法( numberOfSections,numberOfRows和cellForRow ..)来遵守UITableViewDataSource协议。
If you need to know when a cell is tapped or other actions on the tableView you should conform to UITableViewDelegate protocol also. 如果您需要知道何时点击单元格或对tableView进行其他操作,则还应该遵守UITableViewDelegate协议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.