简体   繁体   English

当视图控制器已经继承自基本视图控制器时,如何在视图控制器上创建表视图

[英]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.

相关问题 如何创建基本视图 controller - How to create a base view controller 当数据源和委托与View Controller分开时,如何将Table View的单击项发送到其他View Controller - How to send clicked item of Table View to other view controller when datasource and delegate are separate from View Controller 如何将视图 controller 中的数据传递到表视图 controller? - How to pass the data from view controller to the table view controller? 如何从常规视图控制器推送表视图控制器? - How to push a table view controller from a regular view controller? 使用推视控制器时如何将Uiimage从一个视图控制器传递到其他视图控制器? - How to Passing Uiimage from one view controller to Other view controller when using push view controller used? 在视图控制器上通过命令更新表视图 - Update Table View on command from View Controller 将数据从表视图传递到视图控制器 - Passing data from table view to view controller 表视图单元格从View Controller返回到Table View Controller时“跳转” - Table View Cells “jump” when going back from View Controller to Table View Controller 将数据从表视图传递到视图控制器 - Pass data from table view to view controller 将表视图控制器更改为视图控制器 - Changing a Table View Controller to a View Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM