繁体   English   中英

UITableView不符合协议UITableViewDataSource错误

[英]UITableView does not conform to protocol UITableViewDataSource error

问题1)

我花了几个小时试图弄清楚这个问题,从我读到的内容来看,这是实现UITableView所需的两个函数,但仍然给我标题中的错误:

import UIKit

class UITableView: UIViewController, UITableViewDataSource,UITableViewDelegate
{

    override func viewDidLoad()
    {

        super.viewDidLoad()

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {


    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {

    }

}

问题2)

TableView是我为我的应用程序实现的选项卡式视图控制器的一部分。 我希望我的TableView控制器具有条目,一旦按下将打开另一个ViewController。 我该如何实现这样的目标?

先感谢您

您错误地设置了ViewController。 创建只是UITableView的UITableViewController,或将UITableView添加到UIViewController(就像您差不多完成了)。

didSelectRowAt方法将允许您选择一个新的viewController,但在我的示例中,只有在情节提要中正确设置了它。

UITableViewController选项

class TableViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 0
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 0
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
    // Configure the cell...
    return cell
}

// MARK: - Table view delegate

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: false)
    performSegue(withIdentifier: "SegueIdentifier", sender: self)
}

UIViewController选项
(在此示例中,需要通过情节提要添加UITableView)

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
}

// MARK: - Table view data source

func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 0
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
    // Configure the cell...
    return cell
}

// MARK: - Table view delegate

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: false)
    performSegue(withIdentifier: "SegueIdentifier", sender: self)
}

这是一个示例项目,可从

您不应该使用UITableView来管理数据源。 那应该是UITableViewController的工作,它管理要在UITableView上显示的数据。 通常, View仅应担心如何显示数据,而不要对其进行管理。 请参阅Apple的MVC设计模式

Apple有一个非常不错且完整的Swift App教程 (请参阅显示数据),内容涉及如何使用用情节提要构建的表格视图设置UITableViewController 您也可以在页面末尾下载并运行源项目。 我强烈推荐它。

您必须实现此强制性数据源方法

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "", for: indexPath)
        return cell
    }

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM