swift - 类型 'ViewController' 不符合协议(protocol) 'UITableViewDataSource' 我不知道为什么

标签 swift

我似乎找不到问题所在,我的代码非常简单和基本,但是,这个错误不允许我度过这个简单的阶段。如果我能在这方面得到一些帮助,那就太好了,谢谢。

import UIKit


class ViewController: UIViewController, UITableViewDelegate , UITableViewDataSource

{
    @IBOutlet weak var messageTableView: UITableView!

    var messagesArray: [String] = [String]()
    override func viewDidLoad()
    {
        super.viewDidLoad()     
        self.messageTableView.delegate = self
        self.messageTableView.dataSource = self



 //add some sample data so that we can see something (because the
 array is orginially empty)


        self.messagesArray.append("Test 1")
        self.messagesArray.append("Test 2")
        self.messagesArray.append("Test 3")

    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()

     }

        func tableView(tableView : UITableView , cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

        {
            //create a table cell

            let cell = self.messageTableView.dequeueReusableCellWithIdentifier("MessageCell") as UITableViewCell

            //Customize the cell
            cell.textLabel?.text = self.messagesArray[indexPath.row]

            //Return the cell

            return cell

        }

        func tableView( tableView: UITableView , numberOfRowsInSection section: Int ) 
                return messagesArray.count

        }
    }

最佳答案

tableView:cellForRowAtIndexPath:tableView:numberOfRowsInSection: 嵌套在 didReceiveMemoryWarning 内部。

您需要将倒数第二个右括号移到该行之后:

// Dispose of any resources that can be recreated.

关于swift - 类型 'ViewController' 不符合协议(protocol) 'UITableViewDataSource' 我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31704333/

相关文章:

ios - WKWeb View : How to local storage enable in iOS8

ios - UITableView 静态第一个单元格与 Google SDK

swift - 如何在多个 ViewController 中使用自定义 UIAlertview?

ios - 如何在应用程序中保存凭据以允许自动填充功能?

ios - 无法自动更新 TableView 中的子值

swift - 没有getter的Swift 3 lazy var的作用

ios - tableView 单元格不显示单元格中的任何内容,但它正在控制台中打印内容

swift - 我如何舍入 UIView

ios - 使用方法调配立即更改所有 UIViewController 实例上 iOS13 上的 modalPresentationStyle

Swift 似乎比循环中的 Objective-C 慢