加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 01:56:52 所属栏目:百科 来源:网络整理
导读:我是IOS迅速发展的新手。我曾经和以前的Xcode 6测试版一起工作。 我已经下载了Xcode 6.0.1,我无法得到这个工作Xcode版本:6.0.1 当我尝试运行该示例时,我仍然得到“MyViewController”不会确认协议“UITableViewDataSource”。 有人可以帮帮我吗?我在这个
>我是IOS迅速发展的新手。我曾经和以前的Xcode 6测试版一起工作。
>我已经下载了Xcode 6.0.1,我无法得到这个工作Xcode版本:6.0.1
>当我尝试运行该示例时,我仍然得到“MyViewController”不会确认协议“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,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,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,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")
        }
}

解决方法

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

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委托和数据源方法现在都需要tableView和indexPath值)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读