ios - UITableView 不符合协议(protocol) UITableViewDataSource 错误

标签 ios swift uitableview

问题 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 是我为我的应用程序实现的选项卡式 View Controller 的一部分。我希望我的 TableView Controller 具有一旦按下就会打开另一个 ViewController 的条目。我怎样才能实现这样的事情?

提前谢谢

最佳答案

您的 ViewController 设置不正确。要么创建一个只是 UITableView 的 UITableViewController,要么将 UITableView 添加到 UIViewController(就像您几乎完成的那样)。

didSelectRowAt 方法将允许您转到新的 viewController,但在我的示例中仅当它在 Storyboard 中正确设置时。

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 选项
(在此示例中,需要通过 Storyboard 添加 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

关于ios - UITableView 不符合协议(protocol) UITableViewDataSource 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41998240/

相关文章:

ios - 获取从 UIImagePickerController 中选择的 UIImage 的 URL

ios - 检查枚举中是否存在值

c# - SQLite 限制和排序到 iPhone 上的页面

ios - UITableView 滚动到底部搞砸了 EstimatedRowHeight

ios - Facebook 快速登录

ios - 以编程方式设置约束不适用于我的 TableView

ios - 如何获取用户位置

iphone - iOS,表格 View 过滤选择与搜索控件

ios - 如何使用 Swift 1.2 传递来自 UnsafeMutablePointer 的输入

iphone - 在 TableView 中组合静态和原型(prototype)内容