![](/img/trans.png)
[英]iOS swift 3 - custom table cell, but no data is showing on the tableview
[英]Swift 3: Custom TableView Cell not showing
嘿,所以我试图创建一个自定义单元,并且已经正确链接了故事板中的所有内容,但是由于某种原因,当我运行时它不会显示任何内容。 我尝试对其进行测试,看来自定义单元对象甚至都没有创建。
class customCell : UITableViewCell{
@IBOutlet weak var EventImage: UIImageView!
@IBOutlet weak var type: UILabel!
@IBOutlet weak var rating: UIImageView!
@IBOutlet weak var date: UILabel!
@IBOutlet weak var name: UILabel!
}
class EventHistoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var TableView: UITableView!
let k = ["hi","hey","yo","hello"]
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return k.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.TableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! customCell
cell.name!.text = self.k[index.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
self.TableView.register(customCell.self, forCellReuseIdentifier: "cell")
TableView.delegate = self
TableView.dataSource = self
}
我究竟做错了什么? 程序在到达cell.name!.text = self.k [index.row]时崩溃
代替cell.name!.text
写入cell.name.text
不要注册单元
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: VC_FavouriteCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! VC_FavouriteCell
cell.name.text = k[index.row]
return cell
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.