简体   繁体   English

iOS8 Swift-表视图-控制器不符合协议UITableViewDataSource

[英]iOS8 Swift - Table View - Controller does not conform to protocol UITableViewDataSource

I added another model to my database, and I am essentially re-creating the same methods for this model as the previous one, however this time around I am getting the following error: 我向数据库中添加了另一个模型,并且实际上我正在为该模型重新创建与上一个模型相同的方法,但是这次我遇到了以下错误:

ContactsDetailViewController.swift:11:1: Type 'ContactsDetailViewController' does not conform to protocol 'UITableViewDataSource'

EDIT: I'm also getting these errors, but again, I don't see where the issue is: 编辑:我也收到这些错误,但再次,我看不出问题所在:

/Volumes/BigMan/Code/Swift/BackpackerSpots/UIKit.UITableViewDataSource:3:48: Protocol requires function 'tableView(_:cellForRowAtIndexPath:)' with type '(UITableView, cellForRowAtIndexPath: NSIndexPath) -> UITableViewCell'

/Volumes/BigMan/Code/Swift/BackpackerSpots/BackpackerSpots/ContactsDetailViewController.swift:14:19: Candidate is not a function

Here is my ContactsDetailViewController: 这是我的ContactsDetailViewController:

import UIKit

class ContactsDetailViewController: UIViewController, UITableViewDataSource,
    UITableViewDelegate {
    @IBOutlet var tableView:UITableView!

    var contact:Contact?


    override func viewDidLoad() {
        super.viewDidLoad()

        // customizing background of tableview
        self.tableView.backgroundColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.2)

        // remove extra separators
        self.tableView.tableFooterView = UIView(frame: CGRectZero)

        // change the color of the separator
        self.tableView.separatorColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.8)

        // self-sizing cells
        tableView.estimatedRowHeight = 36.0
        tableView.rowHeight = UITableViewAutomaticDimension

    }


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

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

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

            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as ContactsDetailTableViewCell
            // make cell transparent so background color can be seen
            cell.backgroundColor = UIColor.clearColor()


            switch indexPath.row {
            case 0:
                cell.fieldLabel.text = "Name"
                cell.valueLabel.text = contact?.contactName
            case 1:
                cell.fieldLabel.text = "Email"
                cell.valueLabel.text = contact?.contactEmail
                cell.mapButton.hidden = false
            default:
                cell.fieldLabel.text = ""
                cell.valueLabel.text = ""

            }

            return cell
    }

 }

Here is the ContactsDetailTableViewCell: 这是ContactsDetailTableViewCell:

import UIKit

class ContactsDetailTableViewCell: UITableViewCell {
    @IBOutlet weak var fieldLabel:UILabel!
    @IBOutlet weak var valueLabel:UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

I must be overlooking something relatively simple, but I just don't see it and I've been struggling with this for hours. 我必须忽略一些相对简单的东西,但是我只是看不到它,而我已经为此苦苦挣扎了好几个小时。 Any suggestions would be much appreciated. 任何建议将不胜感激。 Thank you. 谢谢。

I think you should add override : 我认为您应该添加override

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


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

The are few things in the above code I would like to point out. 我想指出以上代码中的几件事。

  1. In cellForRowAtIndexPath method, you are using the cell identifier as "Cell", make sure you are using the same name in Storyboard and its unique across the project. 在cellForRowAtIndexPath方法中,您将单元格标识符用作“ Cell”,请确保在Storyboard中使用相同的名称,并且在整个项目中使用唯一的名称。
  2. In cellForRowAtIndexPath method, brackets are not rightly marked 在cellForRowAtIndexPath方法中,括号未正确标记

Try writing the following line for your cell: 尝试为您的单元格编写以下行:

let cell : ContactsDetailTableViewCell = tableView.dequeueReusableCellWithIdentifier("ContactsDetailTableViewCell", forIndexPath: indexPath) as ContactsDetailTableViewCell

Note: Make sure you rename identifier in the storyboard too. 注意:请确保也在情节提要中重命名标识符。

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

相关问题 iOS:'MyViewController'不符合协议'UITableViewDataSource' - iOS : 'MyViewController' does not conform to protocol 'UITableViewDataSource' ViewController不符合协议UITableViewDataSource - ViewController does not conform to protocol UITableViewDataSource ImagesTabViewController'不符合协议'UITableViewDataSource' - ImagesTabViewController' does not conform to protocol 'UITableViewDataSource' Swift错误类型'usersVC'不符合协议'UITableViewDataSource' - Swift Error type 'usersVC' does not conform to protocol 'UITableViewDataSource' Swift关于视图未确认协议“ UITableViewDataSource” - Swift about view does not confirm 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