【发布时间】:2020-05-11 19:27:06
【问题描述】:
所以我首先制作了一个自定义对象:
import Foundation
class Website {
var name:String
var pictureLabel:String
init(title:String,pictureLabel:String) {
name = title
pictureLabel = picture
}
}
然后在我的 tableviewcontroller 类上:
class ViewController:
UIViewController,UITableViewDelegate,UITableViewDataSource {
var websites = [Website]()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.rowHeight = 80
tableView.register(MyCell.self, forCellReuseIdentifier: "MyCell")
websites.append(Website(title: "facebook.com", pictureLabel: "facelogo"))
print(websites)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return websites.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
cell.label.text = websites[indexPath.row].name
return cell }
错误:致命错误:在隐式展开可选值时意外发现 nil:文件
【问题讨论】:
-
你在哪一行得到错误?
-
您确定您的 MyCell 设置正确吗?它在 Interface Builder(或 Storyboard)中是否有正确的名称 - “MyCell”?
-
不幸的是,您在此处包含的错误消息似乎与您包含的代码无关。您是否正确粘贴了错误?您的项目中的其他地方是否有一个名为“file”的变量?
-
@Mohamedaboghali,我注意到您找到了答案 here 您可以编辑您的帖子或发布您的答案,以便其他有同样问题的人可以解决它吗?
-
我看不出您在此处包含的代码将如何编译。在你的
WebSite类中,你有init(title:,picture:),但你用Website(title:pictureLabel:)调用它。picture和pictureLabel之间不匹配。请更新您的问题以包含当前编译的代码。
标签: arrays swift uitableview