我正在尝试创建一个自定义表格 View ,但偶然发现了每个代码。截至目前,我在下面有这个。这很困惑而且可能是错误的,但有人可以帮忙吗?
此外,我不断收到错误代码
unexpectedly found nil while unwrapping optional value
import UIKit
class tableOutlineViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var names = ["Max", "Bill", "Reagan", "Mikayla", "Jessie", "Sierra", "Jeff", "Erik", "Landon"]
var numbers = ["35", "33", "29", "27", "25", "23", "19", "15", "11"]
var photo = [UIImage(named: "Person1"), UIImage(named: "Person2"), UIImage(named: "Groceries"), UIImage(named: "Person3"), UIImage(named: "Person4"), UIImage(named: "Person5"), UIImage(named: "Person6"), UIImage(named: "Person7")]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
cell.images.image = photo[indexPath.row]
cell.name.text = names[indexPath.row]
cell.number.text = numbers[indexPath.row]
return cell
}
}
最佳答案
错误位于以下行,因为它无法强制展开 CustomCell
:
let cell = self.tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
由于您可能在自定义单元格中使用 Nib
,因此您应该首先在 viewDidLoad
中注册它。
let nib = UINib(nibName: YOUR_CUSTOM_CELL_NIB_NAME, bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "CustomCell")
关于arrays - 自定义TableView错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45395873/