简体   繁体   English

iOS:'MyViewController'不符合协议'UITableViewDataSource'

[英]iOS : 'MyViewController' does not conform to protocol 'UITableViewDataSource'

  • I am new to IOS swift development. 我是IOS swift发展的新手。 I used to work with previous Xcode 6 beta . 我曾经使用过之前的Xcode 6 beta

  • I have downloaded the Xcode 6.0.1 and I can not get this to work Xcode Version: 6.0.1 我已经下载了Xcode 6.0.1 ,我无法使用Xcode Version: 6.0.1

  • I am still getting "' MyViewController ' does not confirm to protocol ' UITableViewDataSource ' " when I try to run the example. 当我尝试运行该示例时,我仍然得到“' MyViewController '没有确认协议' UITableViewDataSource '”。

Can someone please help me? 有人可以帮帮我吗? I have gone through other issues on this site and added all the required functions for " UITableViewDataSource "; 我在这个网站上经历了其他问题,并为“ UITableViewDataSource ”添加了所有必需的功能;


import UIKit
import Foundation

class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{

var array1:[String] = ["one","two","three","four"]

    var array2:[String] = ["IOS","Android","java","c++","Swift"]

    let sectionCount = 2

    var myTableView:UITableView!

    //    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    //        var rect = CGRectMake(0, 0, 220, 320)
    //        myTableView = UITableView(frame: rect, style: UITableViewStyle.Grouped)
    //        super.init(nibName: nil, bundle: nil)
    //        // Custom initialization
    //    }

    override func viewDidLoad() {
        super.viewDidLoad()

        var rect = CGRectMake(0, 0, 320, 600)
        myTableView = UITableView(frame: rect, style: UITableViewStyle.Grouped)
        myTableView!.delegate = self
        myTableView!.dataSource = self
        self.view.addSubview(myTableView)

    }

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


    //dataSourrce
    //tableview:tableview,section:
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
        switch section{
        case 0:
            return array1.count
        case 1:
            return array2.count
        default:
            return 1
        }

    }


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

        //---cellstart----
        let identifier = "identifier"
        //        var cell:UITableViewCell
        //cell
        var cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? MyCell
        if cell == nil {
            //            cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: identifier)
            cell = MyCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: identifier)
        }
        //---cellend----

        switch indexPath.section{
        case 0:
            //            cell!.textLabel.text = array1[indexPath.row]
            cell!.myLable!.text = array1[indexPath.row]
        case 1:
            //            cell!.textLabel.text = array2[indexPath.row]
            cell!.myLable!.text = array2[indexPath.row]
        default:
            println()
        }
        var image = UIImage(named: "images/qq.png")
        //        cell!.imageView.image = image
        cell!.myImageView!.image = image
        //        cell!.detailTextLabel.text = "\(indexPath.section)\(indexPath.row)
        return cell!
    }

  //dataSourrce
    func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
        return sectionCount
    }

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        var title:String? = nil
        switch section {
        case 0:
            title = "Num"
        case 1:
            title = "Prog"
        default:
            title = nil
        }
        return title
    }

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        println("Test\(indexPath.section) \(indexPath.row)")
    }


    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 60.0
    }



    func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {}

    func pickerView(pickerView: UIPickerView!,numberOfRowsInComponent component: Int) -> Int{}

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?{
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?{
    }

}

***************** My Cell Class *********************************** *****************我的细胞课****************************** *****

import Foundation
import UIKit

class MyCell: UITableViewCell {

    let indetifier:String = "indetifier"


    var myLable:UILabel?


    var myImageView:UIImageView?

        override init(style: UITableViewCellStyle, reuseIdentifier: String!)
        {
        super.init(style: .Subtitle, reuseIdentifier: indetifier)

        var rect = CGRectMake(10, 0, 60, 30)
        self.myLable = UILabel()
        self.myLable!.frame = rect
        self.myLable!.textColor = UIColor.redColor()
        self.contentView.addSubview(self.myLable!)

        var imageRect = CGRectMake(160, 10, 40, 40)
        self.myImageView = UIImageView()
        self.myImageView!.frame = imageRect
        self.contentView.addSubview(self.myImageView!)


    }

        required init(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
}

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) 所以...你的numberOfRowsInSection采用一个可选的UITableView ,并且应该采用一个必需的 UITableView (这是他们在6和6.1之间做出的改变,所有UITableView委托和数据源方法现在都采用所需的tableViewindexPath值)

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)
    {

    }

}
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. 当您为UITableView创建子类时,请参考上面的代码,您将不会收到任何错误。

In my case, i named my UITableView as tableView . 在我的例子中,我将我的UITableView命名为tableView That caused this same error: 这导致了同样的错误:

'MyViewController' does not conform to protocol 'UITableViewDataSource' 'MyViewController'不符合协议'UITableViewDataSource'

including another error: 包括另一个错误:

Candidate is not a function 候选人不是一个职能

And changed tableview name to something else, error is gone. 并将tableview名称更改为其他内容,错误消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ViewController不符合协议UITableViewDataSource - ViewController does not conform to protocol UITableViewDataSource 类型MyViewController不符合协议'STPPaymentContextDelegate' - Type MyViewController does not conform to protocol 'STPPaymentContextDelegate' 类型“myViewController”不符合Swift中的协议UIPIckerDataSource - Type “myViewController” does not conform to protocol UIPIckerDataSource in Swift ImagesTabViewController'不符合协议'UITableViewDataSource' - ImagesTabViewController' does not conform to protocol 'UITableViewDataSource' iOS8 Swift-表视图-控制器不符合协议UITableViewDataSource - iOS8 Swift - Table View - Controller does not conform to protocol UITableViewDataSource 我的UIViewController“不符合协议:'UITableViewDataSource'” - My UIViewController “does not conform to protocol: 'UITableViewDataSource'” 类型的viewcontroller不符合协议uitableviewdatasource - type viewcontroller does not conform to protocol uitableviewdatasource 错误:类型“ UserAccView”不符合协议“ UITableViewDataSource” - error : type “UserAccView” does not conform to protocol 'UITableViewDataSource' 自定义UITableViewCell不符合协议UITableViewDataSource吗? - Custom UITableViewCell does not conform to protocol UITableViewDataSource? UITableView不符合协议UITableViewDataSource错误 - UITableView does not conform to protocol UITableViewDataSource error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM