尝试构建应用后,包含cell?.img.image = UIImage (named: name[indexPath.row])
的行显示一条错误消息。它指出:
线程1:致命错误:在隐式展开一个可选值时意外发现nil
我正在使用当前版本的xcode
import UIKit
class ViewController: UIViewController {
var name = ["Avenger", "Apple of My Eye", "Now You See Me", "The Prestige", "Stardust", "The Illusionist"]
@IBOutlet weak var tableView: UITableView!
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return name.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? CellTableViewCell
cell?.lbl.text = name[indexPath.row]
cell?.img.image = UIImage (named: name[indexPath.row])
return cell!
CellTableViewCell.swift
import UIKit
class CellTableViewCell: UITableViewCell {
@IBOutlet weak var lbl: UILabel!
@IBOutlet weak var img: UIImageView!
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
}
}