Swift:表视图超类错误

我用Swift创build了一个滑出菜单。 我以前做过这么多次,但是当我今天创build它的时候,我得到这个错误(见截图)。 这可能只是我犯的一个简单的错误。

这是代码,我认为是造成这个问题:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell if cell == nil{ cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") cell!.backgroundColor = UIColor.clearColor() cell!.textLabel!.textColor = UIColor.darkTextColor() let selectedView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: cell!.frame.size.width, height: cell!.frame.size.height)) selectedView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3) cell!.selectedBackgroundView = selectedView } cell!.textLabel!.text = tableData[indexPath.row] return cell } 

截图:

在这里输入图像说明

更新:我已经尝试删除override

在这里输入图像说明

希望有人能帮助!

该方法不重写超类的任何方法,因为签名是错误的。

正确的签名是

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

所有参数都是非可选types。

并使用推荐的方法

 let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) 

这也总是返回一个非可选types

确保你的类实现了UITableViewDelegateUITableViewDataSource

 class MyController: UIViewController, UITableViewDelegate, UITableViewDataSource { // All your methods here } 

除非任何其他超类已经实现了这些方法,否则不需要override关键字。

看到这个对我有用

  • 只要确认UITableViewDelegateUITableViewDataSource
  • 实施所需的方法

 import UIKit class ListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let containArray = ["One","two","three","four","five"] // MARK: - View Lifecycle override func viewDidLoad() { super.viewDidLoad() } override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //MARK: Table view data source and delegate methods //Number of rows func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return containArray.count } //Prepare Custom Cell func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let identifier = "simpleTextCell" var cell: SimpleTextCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? SimpleTextCell if cell == nil { tableView.registerNib(UINib(nibName: "SimpleTextCell", bundle: nil), forCellReuseIdentifier: identifier) cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? SimpleTextCell } let dayName = containArray[indexPath.row] cell.lblProductName.text = dayName return cell } //Handle click func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { self.performSegueWithIdentifier("productListSegue", sender: self) } } 

试着去掉“!”,这个函数的声明是:

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

并确保你已经将tableview的委托和数据源设置为“self”

Interesting Posts