Ios 为什么我的UITableView不再工作?

Ios 为什么我的UITableView不再工作?,ios,swift,uitableview,Ios,Swift,Uitableview,这个表格一直工作到我添加了两个空单元格。 我不知道发生了什么,但现在我不明白我错过了什么。 我发布了所有的“SchedaVolontario.swift”代码,这样你就可以分析所有的代码了 如您所见,我使用了tableView.dequeueReusableCell方法和indepath构造函数 // SchedaVolontario.swift import UIKit import Firebase import FlexibleSteppedProgressB

这个表格一直工作到我添加了两个空单元格。 我不知道发生了什么,但现在我不明白我错过了什么。 我发布了所有的
“SchedaVolontario.swift”
代码,这样你就可以分析所有的代码了

如您所见,我使用了
tableView.dequeueReusableCell
方法和
indepath
构造函数

 //  SchedaVolontario.swift

    import UIKit
    import Firebase
    import FlexibleSteppedProgressBar

    class SchedaVolontario: UIViewController, UITableViewDataSource, UITableViewDelegate, FlexibleSteppedProgressBarDelegate {


        let userCalendar = Calendar.current

        let requestedComponent: Set<Calendar.Component> = [.day,.hour,.minute,.second,.month]

        var codvolontario : String?
        var nome: String?
        var cognome : String?
        var sede : String?
        var progetto : String?
        var datainizioservizio : String?
        var datafineservizio : String?
        var spazio: String?
        var licenze : String?
        var malattie : String?
        var monitoraggio_uno : String?
        var monitoraggio_due : String?
        var monitoraggio_tre : String?
        var progressBar: FlexibleSteppedProgressBar!
        var monitoraggiostep = 0
        var maxIndex = -1
        // dichiarazione tabella di riferimento
        @IBOutlet weak var guestInfoTableView: UITableView!

        //dichiarazione array celle
        var guestInfoList: [String] = []

        //dichiarazione array celle
        var test: [String] = []

        //Dichairazione variabili generali per l'esecuzione di routine di calcolo

        var labName: UILabel?

        override func viewDidLoad() {
            super.viewDidLoad()


            //aggiorna cella monitoraggio
            ricalcolaCellaMonitoraggio()
            //avvia funzione riempimento celle
            populateGuestInfoList()
            //configurazione countdown
            Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ricalcolaCellaCountdown), userInfo: nil, repeats: true)
            //delegate and datasource
            guestInfoTableView.delegate = self
            guestInfoTableView.dataSource = self
        }


        //riempimento celle
        func populateGuestInfoList() {

            //Nome
            if codvolontario != nil {
                guestInfoList.append("")
            }

            //Cognome
            if codvolontario != nil {
                guestInfoList.append("")
            }

            //Codice volontario
            if codvolontario != nil {
                guestInfoList.append("Il mio Codice Volontario:                     \(codvolontario!)")
            }

            //void1
            if codvolontario != nil {
                guestInfoList.append("")
            }

            //Ente
            if sede != nil {
                guestInfoList.append("Dove svolgo il mio servizio civile:")
            }

            //Descrizione ente
            if let sede = sede {
                guestInfoList.append(sede)
            }

            //Progetto
            if progetto != nil {
                guestInfoList.append("Il mio progetto:")
            }


            //Descrizione titolo progetto
            if let progetto = progetto {
                guestInfoList.append(progetto)
            }

            //void2
            if codvolontario != nil {
                guestInfoList.append("")
            }

            //Tempo
            if spazio == spazio {
                guestInfoList.append("Quanto manca alla fine del mio progetto:")
            }
            //creazione cella vuota per il countdown
            if spazio == spazio {
                guestInfoList.append("")
    }

            //Data inizio
            if  let datainizioservizio = datainizioservizio {
                guestInfoList.append("\(datainizioservizio)")

            }

            //Data fine
            if let datafineservizio = datafineservizio {
                guestInfoList.append("\(datafineservizio)")
            }

            //Monitoraggio
            if  monitoraggio_uno == monitoraggio_uno {
                guestInfoList.append("")
            }


        }



        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return guestInfoList.count
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            //Nome
            if indexPath.row == 0 {
                let nomecell = tableView.dequeueReusableCell(withIdentifier: "nomecell", for: indexPath)
                var myMutableString = NSMutableAttributedString()
                let myString: NSString = "Nome:                                                     \(nome!)" as NSString
                myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Arial", size: 18.0)!])
                myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location:0,length:5))
                // set label Attribute
                labName?.attributedText = myMutableString
                nomecell.textLabel?.font = UIFont.boldSystemFont(ofSize: 12)
                nomecell.textLabel?.text = myString as String?
                nomecell.textLabel?.attributedText = myMutableString
            }

            //Cognome
            if indexPath.row == 1 {
                let cognomecell = tableView.dequeueReusableCell(withIdentifier: "cognomecell", for: indexPath)
                var myMutableString = NSMutableAttributedString()
                let myString: NSString = "Cognome:                                              \(cognome!)" as NSString
                myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Arial", size: 18.0)!])
                myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location:0,length:8))
                // set label Attribute
                labName?.attributedText = myMutableString
                cognomecell.textLabel?.text = myString as String?
                cognomecell.textLabel?.attributedText = myMutableString
            }

            //codvolcell
            if indexPath.row == 2 {
                let codvolcell = tableView.dequeueReusableCell(withIdentifier: "codvolcell", for: indexPath)
                var myMutableString = NSMutableAttributedString()
                let myString: NSString = "Il mio Codice Volontario:                     \(codvolontario!)" as NSString
                myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Arial", size: 18.0)!])
                myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location:0,length:25))
                // set label Attribute
                labName?.attributedText = myMutableString
                codvolcell.textLabel?.text = myString as String?
                codvolcell.textLabel?.attributedText = myMutableString
            }

            //distacco 1
            if indexPath.row == 3 {
                let voidcell = tableView.dequeueReusableCell(withIdentifier: "voidcell", for: indexPath)
                voidcell.textLabel?.text = ""
            }

            //Ente
            if indexPath.row == 4 {
                let entecell = tableView.dequeueReusableCell(withIdentifier: "entecell", for: indexPath)
                entecell.textLabel?.text = "Dove svolgo il mio servizio civile:"
                let myFont: UIFont? = UIFont(name: "Arial", size: 18.0)
                if let aFont = myFont {
                    entecell.textLabel?.font = aFont
                    entecell.textLabel?.textColor = UIColor.red
                }
            }

            //Descrizione ente
            if indexPath.row == 5 {
                let sedecell = tableView.dequeueReusableCell(withIdentifier: "sedecell", for: indexPath)
                sedecell.textLabel?.text = sede
                let myFont: UIFont? = UIFont(name: "Arial", size: 18.0)
                if let aFont = myFont {
                    sedecell.textLabel?.font = aFont
                }
            }

            //Progetto
            if indexPath.row == 6 {
                let progettocell = tableView.dequeueReusableCell(withIdentifier: "progettocell", for: indexPath)
                progettocell.textLabel?.text = "Il mio progetto:"
                let myFont: UIFont? = UIFont(name: "Arial", size: 18.0)
                if let aFont = myFont {
                    progettocell.textLabel?.font = aFont
                    progettocell.textLabel?.textColor = UIColor.red
                }
            }

            //Descrizione titolo progetto
            if indexPath.row == 7 {
                let titolocell = tableView.dequeueReusableCell(withIdentifier: "titolocell", for: indexPath)
                titolocell.textLabel?.text = progetto
                let myFont: UIFont? = UIFont(name: "Arial", size: 18.0)
                if let aFont = myFont {
                    titolocell.textLabel?.font = aFont
                }
            }

            //distacco 2
            if indexPath.row == 8 {
                let voidcell2 = tableView.dequeueReusableCell(withIdentifier: "voidcell2", for: indexPath)
                voidcell2.textLabel?.text = ""
            }

            //Tempo residuo alla fine del SC
            if indexPath.row == 9 {
                let tempocell = tableView.dequeueReusableCell(withIdentifier: "tempocell", for: indexPath)
                tempocell.textLabel?.text = "Quanto manca alla fine del mio progetto:"
                let myFont: UIFont? = UIFont(name: "Arial", size: 18.0)
                if let aFont = myFont {
                    tempocell.textLabel?.font = aFont
                    tempocell.textLabel?.textColor = UIColor.red
                }
            }

            //countdown
            let countdowncell = tableView.dequeueReusableCell(withIdentifier: "countdowncell", for: indexPath)
            if indexPath.row == 10 {
                let countdowncell = tableView.dequeueReusableCell(withIdentifier: "countdowncell", for: indexPath)
                countdowncell.textLabel?.text = calcolaCountdown()
            }else{
                countdowncell.textLabel?.text = guestInfoList[indexPath.row]
            }


            //Data inizio servizio civile
            if indexPath.row == 11 {
                let datainiziocell = tableView.dequeueReusableCell(withIdentifier: "datainiziocell", for: indexPath)
                var myMutableString = NSMutableAttributedString()
                let myString: NSString = "Data inizio del mio servizio civile:                 \(datainizioservizio!)" as NSString
                myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Arial", size: 18.0)!])
                myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location:0,length:36))
                // set label Attribute
                labName?.attributedText = myMutableString
                datainiziocell.textLabel?.font = UIFont.boldSystemFont(ofSize: 12)
                datainiziocell.textLabel?.text = myString as String?
                datainiziocell.textLabel?.attributedText = myMutableString
            }

            //Data fine servizio civile
            if indexPath.row == 12 {
                let datafinecell = tableView.dequeueReusableCell(withIdentifier: "datafinecell", for: indexPath)
                var myMutableString = NSMutableAttributedString()
                let myString: NSString = "Data fine del mio servizio civile:                 \(datafineservizio!)" as NSString
                myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Arial", size: 18.0)!])
                myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location:0,length:34))
                // set label Attribute
                labName?.attributedText = myMutableString
                datafinecell.textLabel?.text = myString as String?
                datafinecell.textLabel?.attributedText = myMutableString
            }

            //Stampa Monitoraggio
            if indexPath.row == 13 {
            let monitoraggiocell = tableView.dequeueReusableCell(withIdentifier: "monitoraggiocell", for: indexPath)

                if monitoraggio_tre ==  "OK" {
                    monitoraggiocell.textLabel?.text = "3° Monitoraggio disponibile"
                    let myFont: UIFont? = UIFont(name: "Arial", size: 18.0)
                    if let aFont = myFont {
                        monitoraggiocell.textLabel?.font = aFont
                    }
                    monitoraggiostep = 2
                    let myFirstButton = UIButton()
                    myFirstButton.setTitle(" ", for: .normal)
                    myFirstButton.setTitleColor(UIColor.blue, for: .normal)
                    myFirstButton.frame = CGRect(x: 345, y: 575, width: 10, height: 10)
                    myFirstButton.addTarget(self, action: #selector(self.pressed(sender:)), for: .touchUpInside)
                    self.view.addSubview(myFirstButton)
                }
                else {
                    if monitoraggio_due == "OK" {
                        monitoraggiocell.textLabel?.text = "2° Monitoraggio disponibile"
                        let myFont: UIFont? = UIFont(name: "Arial", size: 18.0)
                        if let aFont = myFont {
                            monitoraggiocell.textLabel?.font = aFont
                        }
                        monitoraggiostep = 1
                        let myFirstButton = UIButton()
                        myFirstButton.setTitle(" ", for: .normal)
                        myFirstButton.setTitleColor(UIColor.blue, for: .normal)
                        myFirstButton.frame = CGRect(x: 290, y: 575, width: 10, height: 10)
                        myFirstButton.addTarget(self, action: #selector(self.pressed(sender:)), for: .touchUpInside)
                        self.view.addSubview(myFirstButton)
                    }


                    else {
                        if monitoraggio_uno == "OK" {
                            monitoraggiocell.textLabel?.text = "1° Monitoraggio disponibile"
                            let myFont: UIFont? = UIFont(name: "Arial", size: 18.0)
                            if let aFont = myFont {
                                monitoraggiocell.textLabel?.font = aFont
                            }
                            monitoraggiostep = 0
                            let myFirstButton = UIButton()
                            myFirstButton.setTitle(" ", for: .normal)
                            myFirstButton.setTitleColor(UIColor.blue, for: .normal)
                            myFirstButton.frame = CGRect(x: 236, y: 575, width: 10, height: 10)
                            myFirstButton.addTarget(self, action: #selector(self.pressed(sender:)), for: .touchUpInside)
                            self.view.addSubview(myFirstButton)
                        }
                        else {
                            if monitoraggio_uno != "OK" {
                                monitoraggiocell.textLabel?.text = "Nessun monitoraggio disponibile"
                                let myFont: UIFont? = UIFont(name: "Arial", size: 18.0)
                                if let aFont = myFont {
                                    monitoraggiocell.textLabel?.font = aFont
                                }
                            monitoraggiostep = 0
                        }
                    }
                }
                }
                //Configurazioni grafiche
                progressBar = FlexibleSteppedProgressBar()
                progressBar.translatesAutoresizingMaskIntoConstraints = false
                progressBar.numberOfPoints = 3
                progressBar.lineHeight = 9
                progressBar.radius = 15
                progressBar.progressRadius = 25
                progressBar.progressLineHeight = 3
                progressBar.delegate = self
                progressBar.useLastState = true
                progressBar.selectedBackgoundColor = UIColor.green
                progressBar.selectedOuterCircleStrokeColor = UIColor.red
                progressBar.lastStateOuterCircleStrokeColor = UIColor.red
                progressBar.centerLayerDarkBackgroundTextColor = UIColor.red
                progressBar.currentSelectedCenterColor = UIColor.red
                //imposta valore monitoraggio
                progressBar.completedTillIndex = monitoraggiostep
                progressBar.currentIndex = monitoraggiostep
                //inserimento e posizionamento
                progressBar.frame = CGRect(x: 230, y: 11, width: 130, height: 20)
                monitoraggiocell.contentView.addSubview(progressBar)
            }

            return countdowncell
        }


        //Calcolo Countdown
        func calcolaCountdown() -> String!
        {
            var test = ""
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "dd/MM/yy"
            let startTime = Date()
            let endTime = dateFormatter.date(from: datafineservizio!)
            let timeDifference = userCalendar.dateComponents(requestedComponent, from: startTime, to: endTime!)
            let dateLabelOutlet = "\(timeDifference.month!) Mesi \(timeDifference.day!) Giorni \(timeDifference.hour!) Ore \(timeDifference.minute!) Minuti \(timeDifference.second!) Secondi"
            if timeDifference.second! < 0 {
                test = "Servizio terminato"
            }
            else {
                test = dateLabelOutlet
            }
            return test
        }
        func ricalcolaCellaCountdown(){
            let indexPathCountdown = IndexPath(row: 10, section: 0)
            guestInfoTableView.reloadRows(at: [indexPathCountdown], with: .none)
        }
        func ricalcolaCellaMonitoraggio(){
            let indexPathCountdown = IndexPath(row: 13, section: 0)
            guestInfoTableView.reloadRows(at: [indexPathCountdown], with: .none)
        }

        @objc func pressed(sender: UIButton!) {
            guard let url = URL(string: "https://www.amesci.org/mobile/serviziocivile/monitoraggio.htm") else {
                return //be safe
            }

            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.openURL(url)
            }

        }

        //Generazione Barra di caricamento del monitoraggio
        func progressBar(_ progressBar: FlexibleSteppedProgressBar,
                         didSelectItemAtIndex index: Int) {
            progressBar.currentIndex = index
            if index > maxIndex {
                maxIndex = index
                progressBar.completedTillIndex = maxIndex
            }
        }


        func progressBar(_ progressBar: FlexibleSteppedProgressBar,
                         canSelectItemAtIndex index: Int) -> Bool {
            return false
        }
        //rinomina valore step monitoraggio
        func progressBar(_ progressBar: FlexibleSteppedProgressBar,
                         textAtIndex index: Int, position: FlexibleSteppedProgressBarTextLocation) -> String {
            if progressBar == self.progressBar {
                if position == FlexibleSteppedProgressBarTextLocation.center {
                    switch index {
                    case 0: return "1"
                    case 1: return "2"
                    case 2: return "3"
                    default: return "0"
                    }
                }
            }
            return ""
        }


    }
聊天日志:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to dequeue multiple cells for the same index path, which is not allowed. If you really need to dequeue more cells than the table view is requesting, use the -dequeueReusableCellWithIdentifier: method (without an index path). Cell identifier: nomecell, index path: <NSIndexPath: 0x6000008244e0> {length = 2, path = 0 - 0}'
***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“试图为同一索引路径将多个单元格出列,这是不允许的。”。如果确实需要将比表视图请求的更多的单元格出列,请使用-dequeueReusableCellWithIdentifier:方法(不带索引路径)。单元标识符:nomecell,索引路径:{length=2,path=0-0}'

编辑

我通过从零开始重写所有表来解决这个问题。伙计们记住:保存你的项目的备份副本!这救了我:


显然,我在
Main.storyboard
上设置了比代码声明的多一个单元格。

您可能没有为表设置/分配数据源和委托。当数组
guestInfoList
有一些值/元素时,重新加载表

试试这个,看看:

override func viewDidLoad() {
    super.viewDidLoad()
    //aggiorna cella monitoraggio
    ricalcolaCellaMonitoraggio()
    //avvia funzione riempimento celle
    populateGuestInfoList()
    //configurazione countdown
    Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ricalcolaCellaCountdown), userInfo: nil, repeats: true)


     // delegate and datasource
     guestInfoTableView.delegate = self
     guestInfoTableView.dataSource = self


}

请尝试将其放入您的
viewDidLoad()


真奇怪。。。“guestInfoTableView.datasource”出现错误:“UITableView”类型的值没有成员“datasource”datasource->dataSourceLook在此处:右。。。根据我在这里共享的代码,我在openingDo类上声明了委托和数据源。它可以工作。但是
cellForRowAt
中的代码效率非常非常低。对
indepath
使用
开关
,或者至少使用
if-else if
表达式。您将多个单元格出列(每次至少重复倒计时),这就是错误消息。另外,除了倒计时,你似乎不返回单元格。是的,那么我该如何解决这个问题呢?我需要所有的单元格通过避免这种情况并执行
if/else if
不仅
if
,还返回正确的单元格。请参阅编辑,我已解决。
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to dequeue multiple cells for the same index path, which is not allowed. If you really need to dequeue more cells than the table view is requesting, use the -dequeueReusableCellWithIdentifier: method (without an index path). Cell identifier: nomecell, index path: <NSIndexPath: 0x6000008244e0> {length = 2, path = 0 - 0}'
override func viewDidLoad() {
    super.viewDidLoad()
    //aggiorna cella monitoraggio
    ricalcolaCellaMonitoraggio()
    //avvia funzione riempimento celle
    populateGuestInfoList()
    //configurazione countdown
    Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ricalcolaCellaCountdown), userInfo: nil, repeats: true)


     // delegate and datasource
     guestInfoTableView.delegate = self
     guestInfoTableView.dataSource = self


}
guestInfoTableView.dataSource = self
guestInfoTableView.delegate = self