【问题标题】:'UITableViewCell?' does not have a member named 'textLabel''UITableViewCell?'没有名为“textLabel”的成员
【发布时间】:2014-10-02 06:17:19
【问题描述】:

我已经四处寻找这个问题的答案,但没有任何成功。我基本上是按照教程来创建一个简单的待办事项应用程序,许多其他人都在评论与以下相同的错误。作者还没有解决办法。任何帮助,将不胜感激。

我收到错误:'UITableViewCell?'没有名为“textLabel”的成员

到目前为止,这是我的代码:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    // tells iphone what to put in each cell
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 4

}

    // Tells iphone how many cells ther are
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

    cell.textLabel?.text = "table cell content"

    return cell!

}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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


}

【问题讨论】:

  • 您必须创建一个 UITableViewCell 类型的 customCell 文件...然后在该 customCell 文件中制作标签的 IBOutlet 然后您就可以使用它...

标签: ios uitableview swift tableview


【解决方案1】:

我正在学习相同的教程,所以我能感受到你的痛苦! :) 在教程中,这个家伙让你删除并重新创建视图控制器,然后他才忘记提到你需要再次命名你的视图控制器。无论如何,为了避免一些麻烦,只需创建一个新项目,将 Table View 放入视图控制器,右键单击 Table View,并将 dataSource、delegate 和 view 链接到 View Controller。

接下来,这是从 XCode 6.1 开始适用于我的代码。天知道他们接下来会改变什么。但简而言之,您不需要“?”在 textLabel 之后。

import UIKit

class ViewController: UIViewController, UITableViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    var items = ["test 1", "test 2", "test 3", "test 4"]

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

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

        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
        cell.textLabel.text = self.items[indexPath.row]
        return cell
    }


}

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    我今天不得不解决同样/类似的问题。发生这种情况是因为您试图在标准表格视图控制器中使用自定义单元格。您需要在函数中告诉控制器具有自定义名称的单元格应该用作(= 而不是)TableViewCell。然后 Xcode 会知道去哪里寻找名字。

    所以在你键入的 indexPath 的右大括号之后:

    as! TableViewCell
    

    【讨论】:

      【解决方案3】:

      试试这个:

      var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
      
      cell.textLabel?.text = "table cell content"
      
      return cell!
      

      【讨论】:

      • 嗨 @derdida 使用您的代码,我的构建成功但我收到此错误:code2014-10-02 08:49:59.957 table-views2[859:79156] -[UIViewController tableView:numberOfRowsInSection :]: 无法识别的选择器发送到实例 0x7fe130c30300 2014-10-02 08:49:59.961 table-views2[859:79156] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[UIViewController tableView:numberOfRowsInSection: ]: 无法识别的选择器发送到实例 0x7fe130c30300'code
      • 你在使用 TableViewController 吗?还是在带有 TableViewDelegate 的 ViewController 中?
      • 作者在这个练习中使用了视图控制器
      【解决方案4】:

      这应该可以解决问题

      cell?.textLabel?.text = array[indexPath.row]

      我假设您使用的是 6.1,因为这在 6.0.1 中不是问题,但他们在最新版本中再次进行了更改。

      希望这对你有用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-23
        • 2014-11-09
        • 2015-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多