自定义单元格在 TableView 中全白
Custom cell is all white in TableView
我想改进我的视图控制器中的 table 视图,使用自定义单元格。在我的单元格中,我只想显示一个带有 imageView 的图像。
这是我的代码:
class ListaIntroduzioni: UIViewController, UITableViewDataSource, UITableViewDelegate {
var listaIntroduzioni: [structIntroduzione] = []
@IBOutlet weak var tableIntroduzioni: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
listaIntroduzioni = [
structIntroduzione(titolo: "Premessa", nomeTesto: "premsessa", nomeImmagine: "001"),
structIntroduzione(titolo: "Riguardo a Questa Raccolta", nomeTesto: "riguardo", nomeImmagine: "002"),
structIntroduzione(titolo: "Introduzione", nomeTesto: "introduzione", nomeImmagine: "003"),
structIntroduzione(titolo: "Ringraziamenti", nomeTesto: "ringraziamenti", nomeImmagine: "004"),
structIntroduzione(titolo: "Commiato", nomeTesto: "commiato", nomeImmagine: "005"),
structIntroduzione(titolo: "Dedica", nomeTesto: "dedica", nomeImmagine: "006")]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableIntroduzioni.dequeueReusableCell(withIdentifier: "cellaIntroduzioni", for: indexPath) as! cellaListaIntroduzioni
cell.imageView?.image = UIImage(named: listaIntroduzioni[indexPath.row].nomeImmagine)
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SegueIntroduzione" {
if let indexPath = tableIntroduzioni.indexPathForSelectedRow {
let introduzione: structIntroduzione
introduzione = listaIntroduzioni[indexPath.row]
let controller = (segue.destination as! UINavigationController).topViewController as! DettaglioIntroduzioni
controller.dettaglioIntroduzione = introduzione
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
}
我不知道为什么我会得到这个 table 查看白色,任何图像都被白色遮挡。在我的另一个观点中,我写了同样的东西并且效果很好。
我希望有人能帮助我。
谢谢
在viewDidLoad
self.tableIntroduzioni.dataSource = self
self.tableIntroduzioni.delegate = self
同时实施 heightForRow
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
我想改进我的视图控制器中的 table 视图,使用自定义单元格。在我的单元格中,我只想显示一个带有 imageView 的图像。 这是我的代码:
class ListaIntroduzioni: UIViewController, UITableViewDataSource, UITableViewDelegate {
var listaIntroduzioni: [structIntroduzione] = []
@IBOutlet weak var tableIntroduzioni: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
listaIntroduzioni = [
structIntroduzione(titolo: "Premessa", nomeTesto: "premsessa", nomeImmagine: "001"),
structIntroduzione(titolo: "Riguardo a Questa Raccolta", nomeTesto: "riguardo", nomeImmagine: "002"),
structIntroduzione(titolo: "Introduzione", nomeTesto: "introduzione", nomeImmagine: "003"),
structIntroduzione(titolo: "Ringraziamenti", nomeTesto: "ringraziamenti", nomeImmagine: "004"),
structIntroduzione(titolo: "Commiato", nomeTesto: "commiato", nomeImmagine: "005"),
structIntroduzione(titolo: "Dedica", nomeTesto: "dedica", nomeImmagine: "006")]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableIntroduzioni.dequeueReusableCell(withIdentifier: "cellaIntroduzioni", for: indexPath) as! cellaListaIntroduzioni
cell.imageView?.image = UIImage(named: listaIntroduzioni[indexPath.row].nomeImmagine)
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SegueIntroduzione" {
if let indexPath = tableIntroduzioni.indexPathForSelectedRow {
let introduzione: structIntroduzione
introduzione = listaIntroduzioni[indexPath.row]
let controller = (segue.destination as! UINavigationController).topViewController as! DettaglioIntroduzioni
controller.dettaglioIntroduzione = introduzione
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
}
我不知道为什么我会得到这个 table 查看白色,任何图像都被白色遮挡。在我的另一个观点中,我写了同样的东西并且效果很好。 我希望有人能帮助我。 谢谢
在viewDidLoad
self.tableIntroduzioni.dataSource = self
self.tableIntroduzioni.delegate = self
同时实施 heightForRow
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}