gpt4 book ai didi

arrays - 黑屏的tableview

转载 作者:行者123 更新时间:2023-11-28 14:46:17 24 4
gpt4 key购买 nike

我有一个具有 3 个 View 的应用程序。第一个是主菜单,第二个是这个(歌曲名称列表)。我有 800 个标题要显示。我想实现一个显示所有这些标题的表格 View 。当我尝试运行构建时,构建是正确的,但是当我转到 tableview 的 View 时,我在 iPhone 上只看到黑屏。我尝试了很多次,我发现了这一点:如果我从 iPhone 中删除我的应用程序,并且我第一次调整它,一切正常,但如果我尝试运行第二个,屏幕是黑色的。我试过我的 iPhone X 和模拟器。有什么问题吗?

我的第二个问题是:是否可以将我所有的数组存储到另一个文件中而不是在 TableViewController 中?感谢大家。

 import UIKit

class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

//Creo le sezioni
let sections = ["A","B","C","D","E","F","G",]

//Creo i vari Array divisi per lettere
let cantiA: [String] = ["A BRUSA SÜTA ‘L SUSA","A DESTRA DELL’ISONZO","A FERRO FREDDO","A L’È SIRA","A L’OSPEDAL DI GENOVA","A LA MATIN BONURA","A LA MODA D’II MÔNTAGNÔN","A LA TOR VANGA","A MASSAUA","A MEZZANOTTE IN PUNTO","A PLAN CALE IL SORELI","A VAN I BRAVI ALPIN","A’ VAN SISÌLIS","ABANDONO","ABBIAM LE SCARPE GROSSE","ADAMELLO","ADDIO CARA MAMMA","ADDIO MIA BELLA, ADDIO","ADDIO NINETTA","ADESSO DORMI","ADUA","AI COMANDI","AI MORTI PER LA PATRIA","AI PREÂT","AL CJANTE IL GIÂL","AL COLONNELLO TINIVELLA","AL COMANDO DEI NOSTRI UFFICIALI","ALL’ALPINO D’ITALIA","ALL’ARMI, ALL’ARMI BERSAGLIERI","ALLA MATTIN","ALLA MATTINA","ALLA MATTINA SI GH’È ‘L CAFÈ","ALLA PRESA DEL CAVENTO","ALPIN JO MAME","ALPINI","ALPINI DELL’OROBICA","ALPINI IN MONTAGNA","ALPINI MITRAGLIERI","ALPINO DELLA JULIA","ALPINO MIO BELL’ALPINO","ALPINO PARTIGIANO","AMA CHI T’AMA","AMA IL VECCHIO","AMICI MIEI","AMILCARE","AMMORE AMMORE","AN VAL DONDONA","ANCHE MIO PADRE","ANCHE SE PESA","ANCORA ‘STI QUATRO","ANDOUMA PROU","ANDREMO IN FRANCIA","APPENA SUL PASUBIO","APRI LA PORTA","APRILE SENZA UN FIORE","APRITE LE PORTE","ARDA BÈRGHEM","ARSO","ARTIGLIERE","ASCOLTATE AMICI CARI","ATTRAVERSO VALLI E MONTI","AU MONT BLANC","AUGELLIN DI PRIMAVERA","AVE, O VERGINE","AVER ‘NA FIGLIA SOLA SOLETA","AVEVA QUINDICI ANNI","AYAS","AZZURRI MONTI"]

let cantiB: [String] = ["BALDI E FORTI"]

override func viewDidLoad() {
super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("section: \(indexPath.section)")
print("row: \(indexPath.row)")
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}

func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
// Fruit Section
return cantiA.count
case 1:
// Vegetable Section
return cantiB.count
default:
return 0
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Create an object of the dynamic cell “PlainCell”
let cell = tableView.dequeueReusableCell(withIdentifier: "PlainCell", for: indexPath)
// Depending on the section, fill the textLabel with the relevant text
switch indexPath.section {
case 0:
// Fruit Section
cell.textLabel?.text = cantiA[indexPath.row]
break
case 1:
// Vegetable Section
cell.textLabel?.text = cantiB[indexPath.row]
break
default:
break
}
return cell
}

}

enter image description here

enter image description here

最佳答案

我不确定,但我认为您的问题出在第一行,您在其中将 TableViewController 声明为 UIViewController 的子类。 UITableViewController 的子类不应该有一个 UITableView 来填充数据吗?

你有:

class TableViewController: UIViewController, ...

也许这有帮助:

class TableViewController: UITableViewController {

关于arrays - 黑屏的tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50221666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com