ios - 在展开 UITableVewCell 自定义单元格类的 Optional 值时意外发现 nil

标签 ios uitableview swift null option-type

当为 TableView 运行代码时,我试图实例化一个自定义单元格并用值填充它,一切似乎都能正常工作,直到它开始将值放入 socket 。

夫妇注意事项:

  1. 我知道这里首选的方法是使用 “tableView.dequeueReusableCellWithIdentifier”然而在这个 情况我正在采取不同的路线。

  2. 此外,自定义单元格中的导出连接到 Storyboard中的相应单元格。

  3. 已设置适当的代表。

提前致谢。

class FavoritesPropertyViewCell: UITableViewCell {
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
    @IBOutlet weak var cellPropertyImage: UIImageView!
    @IBOutlet weak var cellRoomsValue: UILabel!
    @IBOutlet weak var cellSpaceValue: UILabel!
    @IBOutlet weak var cellPriceRangeValue: UILabel!
    @IBOutlet weak var cellCityStateZipValue: UILabel!
    @IBOutlet weak var cellAddressValue: UILabel!

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

var newPropertyCell: FavoritesPropertyViewCell = FavoritesPropertyViewCell()

newPropertyCell.cellRoomsValue.text = "2 rooms"
newPropertyCell.cellSpaceValue.text = "2000 sqft"
newPropertyCell.cellPriceRangeValue.text = "20,000"
newPropertyCell.cellAddressValue.text = "123 that address" 
newPropertyCell.cellCityStateZipValue.text = "Knoxvegas TN 37777"

return newPropertyCell
}

The variables shown have values in them.

最后,错误: fatal error :在展开可选值时意外发现 nil

最佳答案

var newPropertyCell: FavoritesPropertyViewCell = FavoritesPropertyViewCell()

没有设置任何导出,因为这里没有与 Storyboard(xib)进行通信,您只需实例化该类型。因此它们都是零。

正确的方法是使用 tableView.dequeueReusableCellWithIdentifier,它将为您从 Storyboard(已注册的 xib)加载 UI。

关于ios - 在展开 UITableVewCell 自定义单元格类的 Optional 值时意外发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29392310/

相关文章:

ios - 对安全性极为陌生。如何将 SSL 添加到我的 Web 应用程序?

ios - 允许在 uitableview 中编辑,但不能在一行中编辑

ios - 如何通过选择 UITableViewCell 更改 TabBarController 中的 ViewController?

objective-c - 只显示很少的cell时,是否需要实现可重用的UITableViewCells的出队机制?

ios - UITableView 和 UIButtons swift 中的多个部分

ios - IOS 13.2.3版本TextField文本不显示如何解决?

ios - 如何按需隐藏状态栏?

ios - 在 iOS 中使用 CLLocationmanager 监控 20 多个 BLE 信标区域

ios - Swift 每 5 天重复一次 LocalNotification

swift - 在swift中,它是全局变量还是属性?