ios - 'UILabel ?' does not have a member named ' 文本'

标签 ios swift list option-type

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

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
}

最佳答案

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

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

或者您可以使用可选链:

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

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

相关文章:

objective-c - 如何将 CMSampleBufferRef 转换为 NSData

ios - @noescape、@escaping 和@autoclosure 之间有什么区别?

ios - 如何使用相对于容器调整大小的项目初始化 UICollectionViewController?

ios - 解析本地数据存储仅返回一个对象

swift - 在数据库中存储 token 的 Http post 请求

html - 单击 asp 链接按钮时如何更改 asp 转发器内的列表的 css 类?

c# - RemoveRange() 方法如何在 List<> 中工作?

swift - 将 subview 添加到堆栈 View 以设置背景颜色的函数或类

ios - 谷歌地图不加载内容 swift 4

python:压缩文件迭代器一起提取下一个行