数据存在时可选Nil

时间:2018-03-30 00:48:02

标签: swift

所以我遇到了设置数据的问题。数据存在,但仍然说未找到。不确定为什么会这样。

Link To Picture Of Issue

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! EventCell
    let info = data[indexPath.row]
    print("This is data")
    print(info.address)
    cell.addressLabel.text = info.address
    cell.dateLabel.text = info.dateString
    return cell
}

CELL CLASS

 import UIKit

class EventCell: UITableViewCell {
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var addressLabel: UILabel!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

解答:

    tableView.register(EventCell.self, forCellReuseIdentifier: cellIdentifier)

删除此行可修复代码。

1 个答案:

答案 0 :(得分:-1)

尝试先打开info.adress

if let address = info.address {
    cell.addressLabel?.text = address
}else {
     cell.addressLabel?.text = "Default Text"  

}

重复info.dateString

的程序