【问题标题】:iOS : 'MyViewController' does not conform to protocol 'UITableViewDataSource'iOS:“MyViewController”不符合协议“UITableViewDataSource”
【发布时间】:2014-11-14 03:53:53
【问题描述】:
  • 我是 IOS swift 开发的新手。我曾经和以前的Xcode 6 beta一起工作过。

  • 我已经下载了Xcode 6.0.1,但我无法让它工作Xcode Version: 6.0.1

  • 当我尝试运行示例时,我仍然收到“'MyViewController' does not confirm to protocol '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?{
    }

}

**********************我的单元班************************** ********

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")
        }
}

【问题讨论】:

  • 在您的 numberOfRowsInSection 方法(以及几乎所有其他委托/数据源方法)中,UITableView 不应该是隐式展开的可选项。这在 Xcode 版本之间的某个时间点发生了变化。您应该通过阅读每个新版本的发行说明来了解最新的变化。
  • 嗨对不起,我不确定你所说的“UITableView 不应该是一个隐式展开的可选”是什么意思。如果可能的话,你能纠正上面的代码吗?
  • 请参阅 Apple 关于 Swift 的书的第 83 页。可选项是语言的基本组成部分,您需要了解它们的工作原理。
  • 您好,我不理解第 83 页上的“可选是基本部分”:(.
  • 他的观点是,可选项是 Swift 的一个非常重要的部分,您在使用该语言之前必须了解和理解。尤其是在与 Objective-C 集成时。你需要回去阅读并理解整本书。

标签: ios xcode uitableview swift


【解决方案1】:

您需要查看整个错误消息。此特定消息包含有关缺少哪些方法的其他信息:

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'

所以...您的 numberOfRowsInSection 采用可选的 UITableView,并且应该采用 必需 UITableView(这是他们在 6 和 6.1 之间所做的更改,所有 UITableView 代表和数据源方法现在需要 tableViewindexPath 值)

【讨论】:

    【解决方案2】:

    试试这个简单的解决方案:

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

    【讨论】:

    • 非常感谢!
    【解决方案3】:
    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) {
    
        }
    }
    

    在为UITableView创建子类时,请参考上面的代码,不会报错。

    【讨论】:

      【解决方案4】:

      就我而言,我将UITableView 命名为tableView。这导致了同样的错误:

      'MyViewController' 不符合协议 'UITableViewDataSource'

      包括另一个错误:

      候选人不是函数

      并将tableview 名称更改为其他名称,错误消失了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-09
        • 1970-01-01
        • 1970-01-01
        • 2016-04-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多