简体   繁体   English

UITableView不符合协议UITableViewDataSource错误

[英]UITableView does not conform to protocol UITableViewDataSource error

Question 1) 问题1)

I've spent a few hours trying to figure out this problem, from what I've read those are the two functions needed to implement the UITableView but it still gives me the error in the title: 我花了几个小时试图弄清楚这个问题,从我读到的内容来看,这是实现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
    {

    }

}

Question 2) 问题2)

The TableView is a part of a tabbed view controller that I am implementing for my app. TableView是我为我的应用程序实现的选项卡式视图控制器的一部分。 I want my TableView controller to have entries that once pressed will open another ViewController. 我希望我的TableView控制器具有条目,一旦按下将打开另一个ViewController。 How can I implement something like that? 我该如何实现这样的目标?

Thank you in advance 先感谢您

You have incorrectly set up your ViewController. 您错误地设置了ViewController。 Either create a UITableViewController which is just a UITableView, or add a UITableView to a UIViewController (Like you have almost done). 创建只是UITableView的UITableViewController,或将UITableView添加到UIViewController(就像您差不多完成了)。

The didSelectRowAt method will allow you to segue to a new viewController, but in my examples only if it is set up correctly in a storyboard. didSelectRowAt方法将允许您选择一个新的viewController,但在我的示例中,只有在情节提要中正确设置了它。

UITableViewController option 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 option UIViewController选项
(The UITableView will need to be added through a Storyboard in this example) (在此示例中,需要通过情节提要添加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)
}

Here is an example project to work from 这是一个示例项目,可从

You shouldn't be using UITableView to manage the data source. 您不应该使用UITableView来管理数据源。 That should be the job of UITableViewController which manages data to be displayed on UITableView . 那应该是UITableViewController的工作,它管理要在UITableView上显示的数据。 The View in general should only worry about how to display the data, not manage it. 通常, View仅应担心如何显示数据,而不要对其进行管理。 See MVC design pattern by Apple. 请参阅Apple的MVC设计模式

Apple has a really nice, and complete Swift App tutorial (See Display the Data) that goes through how to set up UITableViewController with a table view you built with storyboard. Apple有一个非常不错且完整的Swift App教程 (请参阅显示数据),内容涉及如何使用用情节提要构建的表格视图设置UITableViewController You can also download and run the source project at the end of the page. 您也可以在页面末尾下载并运行源项目。 I highly recommend it. 我强烈推荐它。

You have to to Implement This Mendatory DataSource Method 您必须实现此强制性数据源方法

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

Hope it Helps. 希望能帮助到你。

暂无
暂无

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

相关问题 错误:类型“ UserAccView”不符合协议“ UITableViewDataSource” - error : type “UserAccView” does not conform to protocol 'UITableViewDataSource' ViewController不符合协议UITableViewDataSource - ViewController does not conform to protocol UITableViewDataSource ImagesTabViewController'不符合协议'UITableViewDataSource' - ImagesTabViewController' does not conform to protocol 'UITableViewDataSource' Xcode类型“ ViewController”中出现错误不符合协议“ UITableViewDataSource” - Getting error in Xcode type “ViewController” does not conform to protocol“UITableViewDataSource” 与“类型'ViewController'不符合协议'UITableViewDataSource'”相关的编译和构建错误 - Compile & Build error related to “Type 'ViewController' does not conform to protocol 'UITableViewDataSource'” Swift错误类型'usersVC'不符合协议'UITableViewDataSource' - Swift Error type 'usersVC' does not conform to protocol 'UITableViewDataSource' 我的UIViewController“不符合协议:'UITableViewDataSource'” - My UIViewController “does not conform to protocol: 'UITableViewDataSource'” 类型的viewcontroller不符合协议uitableviewdatasource - type viewcontroller does not conform to protocol uitableviewdatasource iOS:'MyViewController'不符合协议'UITableViewDataSource' - iOS : 'MyViewController' does not conform to protocol 'UITableViewDataSource' 自定义UITableViewCell不符合协议UITableViewDataSource吗? - Custom UITableViewCell does not conform to protocol UITableViewDataSource?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM