ios - 为扩展 View 添加委托(delegate)和数据源

标签 ios swift uitableview

首先,我不确定它的名称是“扩展 View ”,请看截图,我指的是黄色背景的 View 。

Screenshot

我在这个 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
    }

}

enter image description here

关于ios - 为扩展 View 添加委托(delegate)和数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54965642/

相关文章:

ios - iOS 11 用户是否可以通过部署目标 10.2 显示 ARSCNView?

cocoa-touch - InterfaceBuilder 中的 UITableViewHeaderFooterView

ios - 相当于应用程序扩展中的 UIApplication.shared.preferredContentSizeCategory

ios - 具有动态高度 iOS 的 UITableViewCell

ios - 分页 UICollectionView 单元格

ios - UITableView 单元格内容未正确显示

ios - 为什么我们在iOS中使用出队可重用关键字

ios - 无法上传 iApp 托管内容

ios - 如何通过按一下按钮将 UISlider 缓慢地向左或向右移动?

swift - CFDictionary 不会桥接到 NSDictionary (Swift 2.0/iOS9)