Reputation: 975
class EmployeeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var employees = [PFEmployee]()
var networking = Networking()
var plus: UIBarButtonItem!
var profile: UIBarButtonItem!
var tableView: UITableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addConstraint(NSLayoutConstraint(item: tableView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 0.0))
self.view.addConstraint(NSLayoutConstraint(item: tableView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
self.view.addConstraint(NSLayoutConstraint(item: tableView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0.0))
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return employees.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> CustomTableViewCell {
let cell: CustomTableViewCell! = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "customCell")
cell.nameLabel.text = employees[indexPath.row].name
cell.pointsLabel.text = employees[indexPath.row].numberOfPoints.toString()
cell.roleLabel.text = employees[indexPath.row].jobDesc
cell.commentaryLabel.text = employees[indexPath.row].commentary
return cell
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
For some reason, I get two errors :
Cannot assign a value of type 'EmployeeViewController' to a value of type 'UITableViewDataSource?'
Type 'EmployeeViewController' does not conform to protocol 'UITableViewDataSource.'
I have clearly implemented all of the methods that are required, so what am I doing wrong?
Upvotes: 1
Views: 367
Reputation: 66292
Your method declaration is wrong. Change this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> CustomTableViewCell {
to this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
(Your method must return a UITableViewCell
, not a CustomTableViewCell
.) This implicit casting would be OK in Objective-C but isn't allowed in Swift, where casting must be explicit.
A few unrelated things:
var tableView: UITableView = UITableView()
can simply be let tableView = UITableView()
self.
that you can remove.Upvotes: 1