我有一个三视图的应用程序。第一个是主菜单,第二个是这个(歌曲标题列表)。我有800个头衔要展示。
我想实现一个显示所有标题的tableview。
当我试着运行构建是正确的,但是当我转到tableview的视图时,我在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
    }

}

arrays - 黑屏tableview-LMLPHP
arrays - 黑屏tableview-LMLPHP

最佳答案

我不确定,但我认为您的问题出在第一行,在这里您将TableViewController声明为UIViewController的子类。是否应该是UITableViewController的子类,让UITableView填充数据?
你有:

class TableViewController: UIViewController, ...

或许这有助于:
class TableViewController: UITableViewController {

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

10-12 02:00