iOS8 Swift - Table 视图 - 控制器不符合协议 UITableViewDataSource
iOS8 Swift - Table View - Controller does not conform to protocol UITableViewDataSource
我在我的数据库中添加了另一个模型,我基本上是为这个模型重新创建与前一个模型相同的方法,但是这一次我收到以下错误:
ContactsDetailViewController.swift:11:1: Type 'ContactsDetailViewController' does not conform to protocol 'UITableViewDataSource'
编辑:我也遇到了这些错误,但同样,我看不出问题出在哪里:
/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
这是我的 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
}
}
这是 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
}
}
我一定是忽略了一些相对简单的东西,但我就是没有看到它,而且我已经为此苦苦挣扎了几个小时。任何建议将不胜感激。谢谢。
我认为你应该添加 override
:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
我想指出上面代码中的几件事。
- 在 cellForRowAtIndexPath 方法中,您使用的单元格标识符为 "Cell",确保您在 Storyboard 中使用相同的名称并且它在整个项目中是唯一的。
- 在 cellForRowAtIndexPath 方法中,括号未正确标记
尝试为您的单元格写入以下行:
let cell : ContactsDetailTableViewCell = tableView.dequeueReusableCellWithIdentifier("ContactsDetailTableViewCell", forIndexPath: indexPath) as ContactsDetailTableViewCell
注意:确保您也在故事板中重命名标识符。
我在我的数据库中添加了另一个模型,我基本上是为这个模型重新创建与前一个模型相同的方法,但是这一次我收到以下错误:
ContactsDetailViewController.swift:11:1: Type 'ContactsDetailViewController' does not conform to protocol 'UITableViewDataSource'
编辑:我也遇到了这些错误,但同样,我看不出问题出在哪里:
/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
这是我的 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
}
}
这是 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
}
}
我一定是忽略了一些相对简单的东西,但我就是没有看到它,而且我已经为此苦苦挣扎了几个小时。任何建议将不胜感激。谢谢。
我认为你应该添加 override
:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
我想指出上面代码中的几件事。
- 在 cellForRowAtIndexPath 方法中,您使用的单元格标识符为 "Cell",确保您在 Storyboard 中使用相同的名称并且它在整个项目中是唯一的。
- 在 cellForRowAtIndexPath 方法中,括号未正确标记
尝试为您的单元格写入以下行:
let cell : ContactsDetailTableViewCell = tableView.dequeueReusableCellWithIdentifier("ContactsDetailTableViewCell", forIndexPath: indexPath) as ContactsDetailTableViewCell
注意:确保您也在故事板中重命名标识符。