Kishore Suthar
Kishore Suthar

Reputation: 2983

ViewController does not confirm to protocol UITableViewDataSource

I done all right but still showing error "ViewController does not confirm to protocol" I searched lot on this site i find same question but did't find any right answer i already add connection of UITableView to UITableViewDelegate and UITableViewDataSource but still showing error what should i do

here is my code

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBOutlet var tableView: UITableView!


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

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{

    let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
    cell.textLabel.text="row#\(indexPath.row)"
    cell.detailTextLabel.text="subtitle#\(indexPath.row)"

    return cell

}



}

Upvotes: 0

Views: 388

Answers (2)

dadalar
dadalar

Reputation: 644

You have other errors on your code on cellForRowAtIndexPath method.

UITableViewCell.textLabel and UITableViewCell.detailTextLabel returns optional UILabel, so you need to unwrap them first (meaning add ! after optionals). So change those 2 lines as below and you're good.

cell.textLabel!.text="row#\(indexPath.row)"
cell.detailTextLabel!.text="subtitle#\(indexPath.row)"

Also, method signature for cellForRowAtIndexPath is wrong. Remove the ! on the last UITableViewCell. Should be like this:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

Upvotes: 0

fluidsonic
fluidsonic

Reputation: 4676

Note the removal of the ! in the delegate methods which were wrong here.

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBOutlet var tableView: UITableView!


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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
    cell.textLabel.text="row#\(indexPath.row)"
    cell.detailTextLabel.text="subtitle#\(indexPath.row)"

    return cell

}



}

Upvotes: 1

Related Questions