'UILabel?' 没有名为 'text' 的成员
'UILabel?' does not have a member named 'text'
这是我要创建的待办事项列表应用程序的代码:
import UIKit
class MyTableViewController: UITableViewController {
var ToDoItems = ["Buy Groceries", "Pickup Laundry", "Wash Car", "Return Library Books", "Complete Assignment"]
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "ToDoCell"
let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
cell.textLabel.text = ToDoItems[indexPath.row] //This is where the issue is
return cell
}
您需要在使用前从 Optional 中解包标签。
cell.textLabel?.text = ToDoItems[indexPath.row]
安全的方法是使用可选绑定:
if let label = cell.textLabel {
label.text = ToDoItems[indexPath.row]
}
或者您可以使用可选链接:
cell.textLabel?.text = ToDoItems[indexPath.row]
这是我要创建的待办事项列表应用程序的代码:
import UIKit
class MyTableViewController: UITableViewController {
var ToDoItems = ["Buy Groceries", "Pickup Laundry", "Wash Car", "Return Library Books", "Complete Assignment"]
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "ToDoCell"
let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
cell.textLabel.text = ToDoItems[indexPath.row] //This is where the issue is
return cell
}
您需要在使用前从 Optional 中解包标签。
cell.textLabel?.text = ToDoItems[indexPath.row]
安全的方法是使用可选绑定:
if let label = cell.textLabel {
label.text = ToDoItems[indexPath.row]
}
或者您可以使用可选链接:
cell.textLabel?.text = ToDoItems[indexPath.row]