ViewController不确认协议'UITableViewDataSource' [英] ViewController does not confirm to protocol 'UITableViewDataSource'

查看:91
本文介绍了ViewController不确认协议'UITableViewDataSource'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题被多次询问。

This question was asked multiple times.

http://stackoverflow.com/questions/25581780/type-viewcontroller-does-not-confirm-to-protocol-uitableviewdatasource 

我确实尝试过那里提供的解决方案,但我仍然无法逃脱这个错误。 UITableView的代码在下面

and I did tried the solutions provided over there but still I am not able to get away with this error. Code to UITableView is below

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
    @IBOutlet var tableView: UITableView!
    var data: [String] = ["one","two","three","four"]

    override func viewDidLoad()
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return self.data.count;
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!)
    {
        println("You clicked cell number #\(indexPath.row)!")
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath!) ->  UITableViewCell!
    {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
        cell.textLabel?.text = self.data[indexPath.row]
        return cell
    }
    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

请告诉我在这里做了什么错误。

Please tell what mistake am I doing here.

推荐答案

试试这个只删除函数 numberOfRowsInSection cellForRowAtIndexPath 并再次添加如下函数:

Try this just remove both the function numberOfRowsInSection and cellForRowAtIndexPath and again add this function like :

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

    return self.data.count
}

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

var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
    cell.textLabel?.text = self.data[indexPath.row]
    return cell
}

这对我有用。

这篇关于ViewController不确认协议'UITableViewDataSource'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆