简体   繁体   中英

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.

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.

class ActivityViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

And in my viewDidLoad function

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. These are numberOfRowsInSection and 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参数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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