为什么我的UITableView不再起作用了?

时间:2018-05-03 10:03:40

标签: ios swift uitableview

此表一直有效,直到我添加了2个空单元格。 我不知道发生了什么,但现在我无法理解我错过了什么。 我发布了所有"SchedaVolontario.swift"代码,这样您就可以分析所有代码。

正如您所看到的,我使用tableView.dequeueReusableCell方法与indexPath构造函数。

 //  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 ""
        }


    }

这是我的主要表格配置:

Main.storyboard

修改

这就是显示控制台:

*** 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: countdowncell, index path: <NSIndexPath: 0x604000038760> {length = 2, path = 0 - 0}'

修改

我在if/else声明中将每个单元格投入,因为Larme建议我这样做...结果是一样的。这里是片段和聊天记录,也许我还是不明白:

func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - &gt; 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
    }
    else {
    //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
    }
    else {
    //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
    }
    else {

       ...

        monitoraggiocell.contentView.addSubview(progressBar)
    }
        }
        }
        }
        }
        }
        }
        }
        }
        }
        }
        }
    }
    let the_chosen_one = tableView.dequeueReusableCell(withIdentifier: "nomecell", for: indexPath)
    return the_chosen_one
}

chatlog:

*** 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}'

修改

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

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

2 个答案:

答案 0 :(得分:1)

您可能没有为您的表设置/分配数据源和委托。当数组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


}

答案 1 :(得分:0)

尝试将其放入viewDidLoad()

guestInfoTableView.dataSource = self
guestInfoTableView.delegate = self