简体   繁体   English

无法分配类型的值以键入UITableViewDataSource

[英]Cannot assign a value of type to type UITableViewDataSource

I am getting the following error when trying to assign the value of my table to self. 尝试将表的值分配给self时,出现以下错误。

Cannot assign a value of type 'ActivityViewController' to a value of
type 'UITableViewDataSource?'

The following lines of code are giving me the error above. 以下代码行给了我上面的错误。 I have looked on SO for other similar issues but found nothing with a table view. 我曾在SO上查找其他类似问题,但在表视图中什么也没发现。

class ActivityViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

And in my viewDidLoad function 在我的viewDidLoad函数中

table.dataSource = self
  • I have tried cleaning and rebuilding the project. 我曾尝试清洁和重建该项目。
  • I have tried disconnecting the table from the view controller and re-connecting it as well with no luck. 我试图从视图控制器断开表并重新连接它,但是没有运气。

Full code: 完整代码:

class ActivityViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { // --> Error here

    @IBOutlet var table: UITableView!

    var likersArray = [PFObject]()

    var username = ""

    func refresh() {
        var likersQuery = PFQuery(className: "Post")
        likersArray.removeAll(keepCapacity: true)
        likersQuery.orderByDescending("createdAt")
        likersQuery.findObjectsInBackgroundWithBlock { (likers, error) -> Void in
            if let likers = likers as? [PFObject] {
                self.likersArray = likers
                self.table.reloadData()
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let myBarColor = UIColor(red: 48/256, green: 72/256, blue: 95/256, alpha: 1.0)
        let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0))
        view.backgroundColor = myBarColor
        self.view.addSubview(view)

        refresh()

        table.dataSource = self // --> Error here


    }

此问题是因为ActivityViewController没有实现所需的数据源方法: numberOfRowsInSectioncellForRowAtIndexPath

There are two problems with your code which is why I think your getting these errors. 您的代码有两个问题,这就是为什么我认为您遇到这些错误的原因。 Firstly there appears to be a bracket issue. 首先,似乎存在一个括号问题。 Looks like your closing your class after the refresh function. 看起来像您在刷新功能后关闭了类。 Mismatching brackets can cause all kinds of strange errors. 括号不匹配会导致各种奇怪的错误。

Secondly you also need to conform to the protocol. 其次,您还需要遵守协议。 The UITableViewDataSource protocol specifies two required functions. UITableViewDataSource协议指定了两个必需的功能。 These are numberOfRowsInSection and cellForRowAtIndexPath. 它们是numberOfRowsInSection和cellForRowAtIndexPath。 After all of this, the code should compile. 在完成所有这些操作之后,应编译代码。 Here is my version of your code, created within a playground. 这是我在操场上创建的代码版本。 All appears fine on my machine. 在我的机器上一切正常。 Please note, I have removed your refresh function to make things less complicated. 请注意,我已经删除了您的刷新功能,以使事情变得不那么复杂。

class ActivityViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var table: UITableView!

    var likersArray = [String]()

    var username = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        let myBarColor = UIColor(red: 48/256, green: 72/256, blue: 95/256, alpha: 1.0)
        let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0))
        view.backgroundColor = myBarColor
        self.view.addSubview(view)

        table.dataSource = self
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCellWithIdentifier("CellName", forIndexPath: indexPath)
    }

}

仔细检查您的委托是否继承...由于编写UITabBarDelegate而不是UITableViewDelegate,我面临着同样的问题

@ Welton122提供的答案应该被认为是正确的,因为根本问题是原始UIViewController类声明中缺少UITableViewDelegate,UITableViewDataSource参数。

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

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