【问题标题】:Swift: Table view superclass errorSwift:表视图超类错误
【发布时间】:2016-07-28 10:50:09
【问题描述】:

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

这是我认为导致问题的代码:

    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

希望有人能帮忙!

【问题讨论】:

  • @RDC 感谢您的建议,但没有奏效。查看更新
  • 您是否真的实现了UITableView 代表所需的协议class YourViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { /* ... */ }
  • 我可能是错的,但只是为了完整起见......你是继承UIViewController还是UITableViewController?如果是第二个,是否配置了静态单元格?

标签: ios swift uitableview superclass


【解决方案1】:

该方法没有覆盖超类的任何方法,因为签名错误。

正确的签名是

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

所有参数都是非可选类型。

也使用推荐的方法

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

它也总是返回一个非可选类型

【讨论】:

  • 感谢您的帮助!答案的第一部分已经解决了问题(签名部分)但是让单元格,我得到“未解析的标识符'单元标识符'”有什么想法吗?
  • 使用您的文字 "Cell" 而不是 cellIdentifier 并删除 nil 检查和所有不必要的感叹号。
  • 非常感谢您!你的回答解决了我的问题!谢谢你的帮助! ?
【解决方案2】:

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

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

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

【讨论】:

  • 谢谢。很遗憾你的回答没有解决我的问题
【解决方案3】:

看看这对我有用

  • 只需确认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)
    }
}

【讨论】:

    【解决方案4】:

    确保您的类实现了UITableViewDelegateUITableViewDataSource

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

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

    【讨论】:

    • 非常感谢!您的部分答案已经解决了我的问题,但是当我删除override 时,我得到了使用未解析的标识符'cell Identifier'。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 2011-10-03
    • 1970-01-01
    相关资源
    最近更新 更多