I am new to IOS
swift
development. I used to work with previous Xcode 6 beta
.
I have downloaded the Xco
Try this simple solution:
import UIKit
class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
//MARK: - Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//MARK: - Tableview Delegate & Datasource
func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int
{
return 10
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
}
}
In my case, i named my UITableView
as tableView
. That caused this same error:
'MyViewController' does not conform to protocol 'UITableViewDataSource'
including another error:
Candidate is not a function
And changed tableview
name to something else, error is gone.
class ROTableView: UITableView, UITableViewDelegate {
func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int {
return 10
}
func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
// cell.text = "Row #\(indexPath.row)"
// cell.detailTextLabel.text = "Subtitle #\(indexPath.row)"
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
}
When you create the subclass for UITableView
please refer the above code, you will not get any error.
You need to look at the whole error message. This specific message includes additional information about which methods are missing:
Type 'MyViewController' does not conform to protocol 'UITableViewDataSource'
Protocol requires function 'tableView(_:numberOfRowsInSection:)' with type '(UITableView, numberOfRowsInSection: Int) -> Int'
Candidate has non-matching type '(UITableView!, numberOfRowsInSection: Int) -> Int'
So... your numberOfRowsInSection
takes an optional UITableView
, and should take a required UITableView
(this is a change they made between 6 and 6.1, all UITableView
delegate and datasource methods now take required tableView
and indexPath
values)