首先,我不确定它的名称是“扩展 View ”,请看截图,我指的是黄色背景的 View 。
我在这个 View 中做了一个自定义的 TableView ,并且我做了所有的配置,问题是,最后我必须为其添加委托(delegate)和数据源。
我们将 View 内的表格单元格称为“OptionTableView”。
我将这些行写为 View Controller 的 viewdidload(屏幕截图中位于 iPhone 框架内的 Controller ),
override func viewDidLoad() {
super.viewDidLoad()
OptionTableView.delegate = self
OptionTableView.dataSource = self
}
但它会导致应用程序崩溃并出现以下错误:
Thread 1: signal SIGABRT
因为它是 UiView,所以我无法为其创建一个 UIControlView 类型的新文件。
你能帮我吗?我如何为这个扩展 UIView 或任何被调用的东西添加委托(delegate)和数据源,让我在其中使用 tableview?
非常感谢您的帮助
最佳答案
我认为你没有添加tableview协议(protocol)。我为此编写了代码:-
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var table_view: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
table_view.dataSource = self
table_view.delegate = self
}
}
关于ios - 为扩展 View 添加委托(delegate)和数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54965642/