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