Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

when I output the test result in my application, I get

(当我在应用程序中输出测试结果时,我得到)

fatal error: array index out of range

(致命错误:数组索引超出范围)

In the code I marked the place where the error occurred.

(在代码中,我标记了发生错误的位置。)

What could be the cause of the error?

(错误的原因可能是什么?)

import UIKit
import RealmSwift

class ResultVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var message = ""
    var result = 0
    var testedVerbs = [Verb]()
    var repeatTestedVerbs = [Verb]()

    @IBOutlet weak var resultLabel:UILabel!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var repeatVerbs: UILabel!
    @IBOutlet weak var nextButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.reloadData()
        setUp()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return testedVerbs.isEmpty ? 0 : testedVerbs.count
    }

    private func setUp() {
        nextButton.setUpButton(button: nextButton, color: white, tintColor: black, cornerRadius: 12)

        for verb in testedVerbs {
            if verb.progress <= 0.49 {
                repeatTestedVerbs.append(verb)
            }
        }

        if repeatTestedVerbs.count <= 3 {//testedVerbs.count/20 * 100 {
            view.backgroundColor = orange
            message = "You Can Do Better!"
        } else {
            view.backgroundColor = green
            message = "Good Job!"
        }
        resultLabel.text = "(message)  (result) / (testedVerbs.count)"
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        tableView.backgroundColor = .clear
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! ResultCell
        //        MARK: Index out of range
        let verb = repeatTestedVerbs[indexPath.row]
        cell.infinitiv.text = verb.infinitiv
        cell.translate.text = verb.translate
        cell.level.text = verb.level
        cell.backgroundColor = .clear
        return cell
    }

    static func storyboardInstance() -> ResultVC? {
        let storyboard = UIStoryboard(name: String(describing: self), bundle: nil)
        return storyboard.instantiateInitialViewController() as? ResultVC
    }

    @IBAction func repeatButton(_ sender: UIButton) {
        if let nvc = navigationController {
            nvc.popViewController(animated: true)
        }
    }

    @IBAction func cancel(_ sender:UIButton) {
        if let nvc = navigationController {
            for vc in nvc.viewControllers {
                if vc is ThemeTVC {
                    navigationController?.popToViewController(vc, animated: true)
                    break
                }
            }
        }
    }
}
  ask by Max translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
482 views
Welcome To Ask or Share your Answers For Others

1 Answer

您的numberOfRows基于testedVerbs ,但是您的cellForRowAtrepeatTestedVerbs读取。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...