带黑屏的桌面视图

时间:2018-05-07 20:06:07

标签: arrays swift tableview

我有3个视图的应用程序。第一个是主菜单,第二个是这个(歌曲标题列表)。我有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
    }

}

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

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

你有:

class TableViewController: UIViewController, ...

也许这会有所帮助:

class TableViewController: UITableViewController {