“的UILabel?没有名为'text'的成员

时间:2015-07-01 00:51:02

标签: ios swift list optional

这是我要创建的待办事项列表应用的代码:

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
}

2 个答案:

答案 0 :(得分:4)

安全的方法是使用可选的绑定:

if let label = cell.textLabel {
    label.text = ToDoItems[indexPath.row]
}

或者您可以使用可选链接:

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

答案 1 :(得分:2)

在使用之前,您需要从Optional中解包标签。

fastcgi_finish_request();
相关问题