Trenton Tyler Trenton Tyler - 6 months ago 26
Swift Question

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


}

Answer

This issue is because ActivityViewController doesn't implement required datasource methods: numberOfRowsInSection and cellForRowAtIndexPath