ios - 'UITableViewCell ?' does not have a member named ' 文本标签'

标签 ios uitableview swift tableview

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

我收到错误:'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.
}


}

最佳答案

我正在学习相同的教程,所以我能感受到你的痛苦! :) 在教程中,这个家伙让你删除并重新创建 View Controller ,直到那时他才忘记提到你需要重新命名你的 View Controller 。不管怎样,为了避免麻烦,只需创建一个新项目,将一个 TableView 放入 View Controller 中,右键单击 TableView ,然后将数据源、委托(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
    }


}

希望对您有所帮助。

关于ios - 'UITableViewCell ?' does not have a member named ' 文本标签',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26155369/

相关文章:

ios - 在单元格中切换,不在 tableView 方法中传递正确的 bool 值

iOS7 Xcode 5 升级导致长按崩溃应用程序

ios - Swift 中 nxn 棋盘实现的递归 n 皇后区

ios - 在 ios 中创建一个简单的聊天 View

swift - 如何在 SwiftUI 列表中重新加载数据?

ios - UICollectionView didSelectIotemAt issue

javascript - TabNavigator 将不同的参数从每个屏幕传递到 StackNavigator header

ios - 以编程方式定义的不同行为 NSLayoutConstraints 与 'greaterThanOrEqual' 的 Storyboard 约束

此视频中的 iOS 动画

ios - 将 NSArray 中的 TableView 项目分组到 Swift 中的 JSON 响应中返回